Common string operations in Python include:
-
Concatenation: Joining two or more strings together.
full_name = "John" + " " + "Doe" # "John Doe" -
Repetition: Repeating a string multiple times.
echo = "Hello " * 3 # "Hello Hello Hello " -
Length: Getting the number of characters in a string.
length = len(full_name) # 8 -
Indexing: Accessing individual characters using their position.
first_char = full_name[0] # 'J' last_char = full_name[-1] # 'e' -
Slicing: Extracting a substring from a string.
substring = full_name[0:4] # "John" -
Changing Case: Converting strings to upper or lower case.
upper_case = full_name.upper() # "JOHN DOE" lower_case = full_name.lower() # "john doe" -
Finding Substrings: Checking if a substring exists within a string.
index = full_name.find("Doe") # 5 -
Replacing Substrings: Replacing parts of a string with another string.
new_name = full_name.replace("Doe", "Smith") # "John Smith"
These operations are essential for manipulating and processing text data in Python. If you want to practice these operations, consider exploring related labs on LabEx!
