String Creation:
my_string = "Hello, World!"
String Concatenation:
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
String Multiplication:
str1 = "Hello "
result = str1 * 3 # "Hello Hello Hello "
String Length:
my_string = "Hello, World!"
length = len(my_string) # 13
Accessing a Specific Character:
my_string = "Hello, World!"
char = my_string[0] # "H"
String Slicing:
my_string = "Hello, World!"
slice = my_string[2:7] # "llo, "
Converting All Characters to Upper or Lower Case:
my_string = "Hello, World!"
upper_case = my_string.upper()
lower_case = my_string.lower()
Splitting a String by a Specific Character:
my_string = "Hello, World!"
parts = my_string.split(",")
Stripping Spaces:
my_string = " Hello, World! "
cleaned_string = my_string.strip()
String Formatting:
name = "John"
age = 30
formatted_string = "My name is {} and I am {} years old.".format(name, age)
Replacing Characters in a String:
my_string = "Hello, World!"
new_string = my_string.replace("o", "e") # "Helle, Werld!"
Finding the Index of a Specific Character:
my_string = "Hello, World!"
index = my_string.index("o") # 4