Skip to main content
In this article, we review several useful Python string methods that create new strings from a source string without modifying the original. These methods are essential for effective string manipulation in your Python projects and can help you build cleaner and more readable code.
All string methods demonstrated here return new string objects. The original strings remain unchanged throughout the transformations.

Capitalize

The capitalize method converts the first character of the string to uppercase (if it’s alphabetical) and converts all remaining characters to lowercase. Example usage:
Expected output:

Center

The center method returns a new string padded with spaces or an optional specified character. The first parameter represents the total width of the resulting string. Example usage:
Expected output:
When providing a second argument to the center method, it uses that character for padding instead of the default space.

Combined Example for Capitalize and Center

The following example demonstrates using both the capitalize and center methods in a single script:
Console output:

Endswith and Startswith

The endswith and startswith methods are used to verify whether a string ends or begins with a specified substring, respectively. Both methods return True or False. Example usage:
Expected output:

Find and rfind

The find method returns the index of the first occurrence of a substring, and it returns -1 if the substring is not found. You can also specify start and end indices for the search. Conversely, the rfind method searches from the right side of the string. Example usage:
Searching from the right:

Alphanumeric, Alphabetic, and Digit Checks

Python provides several methods to check the composition of strings:
  • isalnum(): Returns True if all characters are alphanumeric.
  • isalpha(): Returns True if all characters are letters.
  • isdigit(): Returns True if all characters are digits.
Example usage:
Expected output:

Case and Whitespace Checks

The following methods are used to verify the case of characters or the presence of whitespace:
  • islower(): Returns True if all alphabetic characters are lowercase.
  • isupper(): Returns True if all alphabetic characters are uppercase.
  • isspace(): Returns True if the string consists solely of whitespace characters.
Example usage:
Expected output:

Join and Split

Join

The join method concatenates an iterable of strings using the string on which it is invoked as a separator. Ensure every element in the list is a string. Example usage:
Expected output:

Split

The split method breaks a string into a list of substrings using a specified delimiter. If no delimiter is provided, it splits the string at whitespace. Example usage:
Expected output:

Lower and Upper

The lower method converts all uppercase letters in a string to lowercase, while the upper method converts all lowercase letters to uppercase. Example usage for lowercase conversion:
Expected output:
Example usage for uppercase conversion:
Expected output:

Stripping Whitespace and Characters

Python provides three primary methods to remove characters from strings:
  • lstrip(): Removes leading whitespace or specified characters.
  • rstrip(): Removes trailing whitespace or specified characters.
  • strip(): Removes both leading and trailing whitespace or specified characters.

Using lstrip

Example usage:
Expected output:

Using rstrip

Example usage:
Expected output:

Using strip

Example usage:
Expected output:

Replace, Swapcase, and Title

Replace

The replace method generates a new string by replacing all occurrences of a specified substring with another substring. You can also limit the number of replacements by providing an optional third parameter. Example usage:
Expected output:

Swapcase

The swapcase method swaps the case of each letter in the string, so uppercase becomes lowercase and vice versa. Example usage:
Expected output:

Title

The title method capitalizes the first character of every word and converts the rest to lowercase. Example usage:
Expected output:
That concludes our comprehensive guide on Python string methods. Start practicing these methods in your coding exercises and projects to improve your string manipulation skills. For more Python tutorials and examples, check out additional Python documentation.

Watch Video

Practice Lab