Python Modify Strings

2 min read ·

In Python, strings come with many built-in methods that allow you to modify, clean, and transform text easily. Since strings are immutable, modification always returns a new string instead of changing the original one.

Upper Case (upper())

The upper() method converts all characters in a string to uppercase.

Lower Case (lower())

The lower() method converts all characters to lowercase.

Remove Whitespace (strip())

The strip() method removes extra spaces from the beginning and end of a string.

Replace String (replace())

The replace() method replaces a part of the string with another string.

Split String (split())

The split() method breaks a string into a list based on a separator.

Capitalize First Letter (capitalize())

The capitalize() method converts the first character to uppercase.

Title Case (title())

The title() method converts the first letter of each word to uppercase.

Remove Specific Characters (lstrip() and rstrip())

  • lstrip() removes characters from the left
  • rstrip() removes characters from the right

Check String Content (Useful Methods)


String Modification Does Not Change Original String


Exercise

  • Convert your name to uppercase
  • Remove extra spaces from a string
  • Replace a word in a sentence
  • Split a sentence into words