Python has some built-in functions, and by using them we can modify the string.
The upper()
function returns the string in upper case.
message = "Hello world"
print(message.upper())
HELLO WORLD
You can see that all characters in the "Hello world"
string characters were converted to uppercase letters.
The lower()
function returns the string in lower case.
message = "HELLO WORLD"
print(message.lower())
hello world
You can see that all characters in the "HELLO WORLD"
string characters were converted to lowercase letters.
The whitespace is the space before and after the actual string text, and by using the strip function, you can remove those white spaces.
message = " Hello world "
print(message.strip())
Hello world
You can see in this example that the white spaces have been removed from the string text.
Using the replace()
method, you can replace a string with another string.
message = "Hello world"
print(message.replace("Hello", "Welcome"))
Welcome world
In this example, we replaced the Hello
text with Welcome
by using the replace function.
The split()
method splits a string text and returns a list with string words.
message = "Hello world"
print(message.split(" "))
['Hello', 'world']
In the example, the string Hello world
was split using the split function, with a space between each word.