Python – Modify Strings

Upper Case

In Python, the upper() method is used to convert all characters in a string to uppercase. It returns a new string and does not modify the original one.

text = "Hello, World!"
upper_text = text.upper()
print(upper_text)

Do it

Lower Case

The lower() method in Python returns a new string where all the characters are converted to lowercase.

text = "Python Is FUN!"
lower_text = text.lower()
print(lower_text)

Do it

Remove Whitespace

To remove whitespace in Python, you can use the strip(), lstrip(), and rstrip() methods. These are especially useful when cleaning up user input or data from files.

text = "   Hello, World!   "
clean_text = text.strip()
print(clean_text)

Do it