Skip to content

Latest commit

 

History

History
173 lines (134 loc) · 3.76 KB

File metadata and controls

173 lines (134 loc) · 3.76 KB
📚 Python String Operations: A Comprehensive Guide
Prepared by: Md Noushad Jahan Ramim*  
Contact:contactwithnoushad@gmail.com



🔍 Introduction to Strings
A string is a data type in Python that stores a sequence of characters.

📝 Defining Strings
Strings can be defined in multiple ways:
- Double Quotes: `"This is a string"`
- Single Quotes: `'This is also a string'`
- Triple Quotes: `'''This is a string'''` (Used for multi-line strings)
- Double Triple Quotes: `"""This is a string"""`

Example:  
str1 = "this is a string \n.we are creating this"
print(str1)

Output:

this is a string
.we are creating this


🔄 Operations on Strings

1️⃣ Concatenation

Definition: Concatenation is the process of combining two or more strings.
Example:

str1 = "noushad"
str2 = "ramim"
finalstr = str1 + str2
print(finalstr)

Output:

noushadramim


2️⃣ Length of a String

Definition: The len() function calculates the total number of characters in a string, including spaces.
Example:

str1 = "noushad"
str2 = "ramim"
finalstr = str1 + " " + str2
print(len(finalstr))

Output:

13


3️⃣ String Indexing

Definition: Access a specific character from a string using its position.

  • Indexing starts at 0.
  • Example:
str = "noushad ramim"
print(str[7])

Output:

r


4️⃣ String Slicing

Definition: Extract specific portions of a string using [start:end] notation.

  • The start index is inclusive.
  • The end index is exclusive.

Examples:

str = "md noushad jahan ramim"

print(str[3:8])   # "noush"
print(str[:8])    # "md noush"
print(str[3:])    # "noushad jahan ramim"
print(str[:])     # "md noushad jahan ramim"
print(str[::2])   # "m osa aa ai"
print(str[5:len(str)])  # "ushad jahan ramim"

5️⃣ Negative Indexing

Definition: Access characters from the end of the string using negative indexes.

Examples:

str = "apple"

print(str[-1])    # "e"
print(str[-5:-1]) # "appl"
print(str[-5:])   # "apple"

✨ String Functions

Python provides several built-in functions for string manipulation:

1️⃣ str.endswith(substring)

Checks if a string ends with a specific substring.

str = "i am a coder"
print(str.endswith("er"))  # True

2️⃣ str.startswith(substring)

Checks if a string starts with a specific substring.

print(str.startswith("am"))  # False

3️⃣ str.capitalize()

Capitalizes the first character of the string.

str = str.capitalize()
print(str)  # "I am a coder"

4️⃣ str.replace(old, new)

Replaces all occurrences of a substring with another substring.

print(str.replace("coder", "developer"))  # "I am a developer"

5️⃣ str.find(substring)

Finds the index of the first occurrence of a substring.

print(str.find("a"))  # 2
print(str.find("x"))  # -1 (not found)

6️⃣ str.count(substring)

Counts the number of occurrences of a substring.

print(str.count("a"))  # 2

🧑‍💻 Practice Problems

Problem 1: Calculate the Length of a Name

Write a program to input a user's first name and print its length.

name = input("Enter your name: ")
print("Your name length is", len(name))

Problem 2: Count $ in a String

Write a program to find the occurrence of $ in a string.

str = "my $ name $ is $ noushad $"
print("$ counts:", str.count("$"))

🎉 Conclusion

Strings are one of the most versatile data types in Python. With the operations and functions discussed, you can manipulate strings effectively and solve real-world problems. Keep practicing to master Python strings!