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.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
All string methods demonstrated here return new string objects. The original strings remain unchanged throughout the transformations.
Capitalize
Thecapitalize method converts the first character of the string to uppercase (if it’s alphabetical) and converts all remaining characters to lowercase.
Example usage:
Center
Thecenter 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:
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 thecapitalize and center methods in a single script:
Endswith and Startswith
Theendswith 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:
Find and rfind
Thefind 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:
Alphanumeric, Alphabetic, and Digit Checks
Python provides several methods to check the composition of strings:isalnum(): ReturnsTrueif all characters are alphanumeric.isalpha(): ReturnsTrueif all characters are letters.isdigit(): ReturnsTrueif all characters are digits.
Case and Whitespace Checks
The following methods are used to verify the case of characters or the presence of whitespace:islower(): ReturnsTrueif all alphabetic characters are lowercase.isupper(): ReturnsTrueif all alphabetic characters are uppercase.isspace(): ReturnsTrueif the string consists solely of whitespace characters.
Join and Split
Join
Thejoin 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:
Split
Thesplit 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:
Lower and Upper
Thelower 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:
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:Using rstrip
Example usage:Using strip
Example usage:Replace, Swapcase, and Title
Replace
Thereplace 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:
Swapcase
Theswapcase method swaps the case of each letter in the string, so uppercase becomes lowercase and vice versa.
Example usage:
Title
Thetitle method capitalizes the first character of every word and converts the rest to lowercase.
Example usage: