Python - Modifying Strings
Learn how to modify strings in Python using built-in methods. Discover how the upper()
method converts a string to uppercase letters. Check out the example to see how it transforms the text.
Python - Modify Strings
Python provides several built-in methods that you can use to modify strings.
Upper Case
The upper()
method converts a string to upper case letters:
Example
a = "hello, everyone!"
print(a.upper())
Output
HELLO, EVERYONE
Lower Case
The lower()
method converts a string to lower case letters:
Example
a = "HELLO, EVERYONE!"
print(a.lower())
Output
hello, everyone!
Remove Whitespace
Whitespace is the space before and/or after the actual text, and very often you want to remove this space.
The strip()
method removes any whitespace from the beginning or the end of a string:
Example
a = " Hello, Everyone! "
print(a.strip())
Output
"Hello, Everyone!"
Replace String
The replace()
method replaces a specified substring with another substring:
Example
a = "Hello, Everyone!"
print(a.replace("H", "G"))
Output
Gello, Everyone!
Split String
The split()
method splits a string into a list of substrings based on a specified separator:
Example
a = "Hello, Everyone!"
print(a.split(","))
Output
'Hello', ' Everyone!'