Python string methods are built-in functions that allow us to perform different operations on strings, such as changing letter case, searching text, replacing characters, splitting strings and checking string properties. Since strings are immutable in Python, these methods return a new string instead of modifying the original one.
lower()
Converts all uppercase characters in a string to lowercase.
Syntax: string_name.lower()
In the code below, we will convert a string to lowercase.
text = "Hello PYTHON"
print(text.lower())
Output
hello python
upper()
Converts all lowercase characters in a string to uppercase.
Syntax: string_name.upper()
In the code below, we will convert a string to uppercase.
text = "Hello Python"
print(text.upper())
Output
HELLO PYTHON
capitalize()
Converts the first character of the string to uppercase and the remaining characters to lowercase.
Syntax: string_name.capitalize()
In the code below, we will capitalize the first character of a string.
text = "python programming"
print(text.capitalize())
Output
Python programming
replace()
Replaces all occurrences of a substring with another substring.
Syntax: string_name.replace(old, new)
In the code below, we will replace one word with another.
text = "I like Java"
print(text.replace("Java", "Python"))
Output
I like Python
split()
Splits a string into a list using the specified separator.
Syntax: string_name.split(separator)
In the code below, we will split a sentence into words.
text = "Python is easy"
print(text.split())
Output
['Python', 'is', 'easy']
join()
Joins elements of an iterable into a single string.
Syntax: separator.join(iterable)
In the code below, we will join a list of words using a hyphen.
words = ["Python", "is", "easy"]
print("-".join(words))
Output
Python-is-easy
find()
Returns the index of the first occurrence of a substring. Returns -1 if the substring is not found.
Syntax: string_name.find(substring)
In the code below, we will find the position of a word.
text = "Learn Python"
print(text.find("Python"))
Output
6
count()
Returns the number of times a substring appears in the string.
Syntax: string_name.count(substring)
In the code below, we will count the occurrences of a word.
text = "apple apple mango"
print(text.count("apple"))
Output
2
strip()
Removes leading and trailing whitespace from a string.
Syntax: string_name.strip()
In the code below, we will remove extra spaces.
text = " Python "
print(text.strip())
Output
Python
startswith()
Checks whether a string starts with the specified prefix.
Syntax: string_name.startswith(prefix)
In the code below, we will check whether the string starts with "Py".
text = "Python"
print(text.startswith("Py"))
Output
True
More String Methods
The following built-in string methods are also available in Python:
| Method | Description |
|---|---|
| casefold() | Converts the string to lowercase for case-insensitive comparisons. |
| center() | Centers the string within the specified width. |
| encode() | Encodes the string using the specified encoding. |
| endswith() | Checks whether the string ends with the specified suffix. |
| expandtabs() | Replaces tab characters with spaces. |
| format() | Formats a string using the given values. |
| format_map() | Formats a string using a dictionary. |
| index() | Returns the index of a substring or raises an error if not found. |
| isalnum() | Returns True if all characters are alphanumeric. |
| isalpha() | Returns True if all characters are alphabetic. |
| isascii() | Returns True if all characters are ASCII characters. |
| isdecimal() | Returns True if all characters are decimal digits. |
| isdigit() | Returns True if all characters are digits. |
| isidentifier() | Checks whether the string is a valid Python identifier. |
| islower() | Returns True if all alphabetic characters are lowercase. |
| isnumeric() | Returns True if all characters are numeric. |
| isprintable() | Returns True if all characters are printable. |
| isspace() | Returns True if the string contains only whitespace. |
| istitle() | Returns True if the string is in title case. |
| isupper() | Returns True if all alphabetic characters are uppercase. |
| ljust() | Left-aligns the string within the specified width. |
| lstrip() | Removes leading characters or whitespace. |
| maketrans() | Creates a translation table for translate(). |
| partition() | Splits the string into three parts at the first separator. |
| removeprefix() | Removes the specified prefix if present. |
| removesuffix() | Removes the specified suffix if present. |
| rfind() | Returns the last occurrence of a substring or -1 if not found. |
| rindex() | Returns the last occurrence of a substring or raises an error. |
| rjust() | Right-aligns the string within the specified width. |
| rpartition() | Splits the string into three parts at the last separator. |
| rsplit() | Splits the string from the right. |
| rstrip() | Removes trailing characters or whitespace. |
| splitlines() | Splits the string into lines. |
| swapcase() | Swaps uppercase and lowercase characters. |
| title() | Converts the first letter of each word to uppercase. |
| translate() | Replaces characters using a translation table. |
| zfill() | Pads the string on the left with zeros until it reaches the specified width. |
For more information about Strings, refer to Python String Tutorial.