Modifying Strings in Python
Strings in Python come with many built-in methods ready for you to use.
Upper Case
Example
Prompt
Demonstrate Python upper case.
Output: complete Python program
main.py
a = "Hello, World!"
print(a.upper())
Lower Case
Example
Prompt
Demonstrate Python lower case.
Output: complete Python program
main.py
a = "Hello, World!"
print(a.lower())
Remove Whitespace
Whitespace means any spaces sitting before and/or after the real text. You will often want to get rid of them.
Example
Prompt
Demonstrate Python remove whitespace.
Output: complete Python program
main.py
a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"
Replace String
Example
Prompt
Demonstrate Python replace string.
Output: complete Python program
main.py
a = "Hello, World!"
print(a.replace("H", "J"))
Split String
The split() method cuts a string at the separator you give it and returns the pieces as a list.
Example
Prompt
Demonstrate Python split string.
Output: complete Python program
main.py
a = "Hello, World!"
print(a.split(","))