📚 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
Definition: Concatenation is the process of combining two or more strings.
Example:
str1 = "noushad"
str2 = "ramim"
finalstr = str1 + str2
print(finalstr)Output:
noushadramim
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
Definition: Access a specific character from a string using its position.
- Indexing starts at 0.
- Example:
str = "noushad ramim"
print(str[7])Output:
r
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"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"Python provides several built-in functions for string manipulation:
Checks if a string ends with a specific substring.
str = "i am a coder"
print(str.endswith("er")) # TrueChecks if a string starts with a specific substring.
print(str.startswith("am")) # FalseCapitalizes the first character of the string.
str = str.capitalize()
print(str) # "I am a coder"Replaces all occurrences of a substring with another substring.
print(str.replace("coder", "developer")) # "I am a developer"Finds the index of the first occurrence of a substring.
print(str.find("a")) # 2
print(str.find("x")) # -1 (not found)Counts the number of occurrences of a substring.
print(str.count("a")) # 2Write 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))Write a program to find the occurrence of $ in a string.
str = "my $ name $ is $ noushad $"
print("$ counts:", str.count("$"))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!