More String Methods
There are many more string methods that we can use to manipulate strings. Here are some examples:
lower() and upper()
The lower()
method converts a string to lowercase and the upper()
method converts a string to uppercase.
string = "Hello World"
lowercase = string.lower()
uppercase = string.upper()
print(lowercase) ## Output: hello world
print(uppercase) ## Output: HELLO WORLD
replace()
The replace()
method replaces all occurrences of a specified string with another string.
string = "Hello World"
new_string = string.replace("World", "Universe")
print(new_string) ## Output: Hello Universe
split()
The split()
method splits a string into a list of substrings based on a specified separator.
string = "Hello World"
substrings = string.split(" ")
print(substrings) ## Output: ['Hello', 'World']
strip()
The strip()
method removes leading and trailing whitespace from a string.
string = " Hello World "
new_string = string.strip()
print(new_string) ## Output: Hello World
There are many more string methods that you can visit in Python documentation for more information.
I hope these examples are helpful! Let me know if you have any questions.