Python -Modify Strings
Python has a set of built-in methods that you can use on strings.
Upper Case
Example
Theupper()
method returns the string in upper case:
print(a.upper())
Lower Case
Example
Thelower()
method returns the string in lower case:
print(a.lower())
Remove Whitespace
Whitespace is the space before and/or after the actual text, and very often you want to remove this space.
Example
Thestrip()
method removes any whitespace from the beginning or the end:
print(a.strip()) # returns "Hello, World!"
Replace String
Example
Thereplace()
method replaces a string with another string:
print(a.replace("H", "J"))
Split String
Thesplit()
method returns a list where the text between the specified separator becomes the list items.
Example
Thesplit()
method splits the string into substrings if it finds instances of the separator:
print(a.split(",")) # returns ['Hello', ' World!']
Learn more about Lists in ourPython Lists chapter.
String Methods
Learn more about String Methods with ourString Methods Reference