25 Essential Python String Methods You Need to Know

Python strings are essential when it comes to working with text and manipulating data. They allow us to store and process textual information in our programs.

Here I will explain the 25 Essential Python String Methods that you should know if you are a Python programmer.

MethodDescription
len()Finds the length of a string.
lower()Converts a string to lowercase.
upper()Converts a string to uppercase.
strip()Removes leading and trailing whitespace from a string.
split()Splits a string into a list of substrings based on a delimiter.
join()Joins a list of strings into a single string using a delimiter.
replace()Replaces a substring with another substring in a string.
startswith()Checks if a string starts with a specific substring.
endswith()Checks if a string ends with a specific substring.
find()Finds the index of the first occurrence of a substring in a string.
index()Finds the index of a substring in a string (raises an error if not found).
count()Counts the number of occurrences of a substring in a string.
isdigit()Checks if a string consists only of digits.
isalpha()Checks if a string consists only of alphabetic characters.
isalnum()Checks if a string consists only of alphanumeric characters.
capitalize()Converts the first character of a string to uppercase and the rest to lowercase.
title()Converts the first character of each word in a string to uppercase.
islower()Checks if all characters in a string are lowercase.
isupper()Checks if all characters in a string are uppercase.
isspace()Checks if a string consists only of whitespace characters.
istitle()Checks if a string is in title case (each word starts with an uppercase letter).
isnumeric()Checks if a string consists only of numeric characters.
isdecimal()Checks if a string consists only of decimal characters (0-9).
encode()Encodes a string into a specified encoding format.
decode()Decodes a string from a specified encoding format.

len()

The len() function in Python is used to determine the length of a string. It allows you to find out how many characters are present in a given string.

To use len(), you simply pass the string as an argument inside the interruption. The function then returns an integer value that represents the length of the string.

Example of len()

string = "You are reading a Blog "
length = len(string)
print(length)  # Output: 23

To find out the length of the string, we use the len() function in Python. It’s like asking Python, “Tell me how many characters are in this string?” The len() function counts all the characters in the string and returns a number that represents the length.

When we print the value of length, we can see the output: 23. This means that the string “You are reading a Blog ” has a length of 23 characters.

lower()

The lower() method in Python is used to convert a string to lowercase. It allows you to transform all the uppercase characters in a string to their lowercase.

To use lower(), you simply call it on a string object or a string literal. The method returns a new string with all the characters converted to lowercase.

Example of lower()

string = "You are reading a Blog "
lowercase_string = string.lower()
print(lowercase_string)  # Output: you are reading a blog 

By calling lower() on the string variable, we create a new string called lowercase_string that contains the original string with all the uppercase characters transformed to lowercase. So, the resulting lowercase_string will be “you are reading a blog “.

upper()

 string = "You are reading a Blog "
upper_string = string.upper()
print(upper_string)  # Output: YOU ARE READING A BLOG 

strip()

The strip() method is used to remove leading and trailing whitespace (spaces, tabs, and newlines) from a string. It helps in cleaning up the string by eliminating any unnecessary whitespace characters at the beginning or end.

In this example, the strip() method is called on the string variable. It removes the extra spaces from both ends of the string, resulting in the string “You are reading a Blog” without any leading or trailing whitespace.

string = "   You are reading a Blog   "
stripped_string = string.strip()
print(stripped_string)  # Output: "You are reading a Blog"

split()

The split() method in Python is used to split a string into a list of substrings based on a specified delimiter. It breaks down a string into multiple parts wherever the delimiter occurs.

In this example, the split() method is called on the string variable without passing any arguments. By default, split() splits the string wherever it encounters whitespace (spaces, tabs, and newlines). As a result, the string is split into a list of substrings at each space, resulting in split_string containing [‘You’, ‘are’, ‘reading’, ‘a’, ‘Blog’].

string = "You are reading a Blog"
split_string = string.split()
print(split_string)  # Output: ['You', 'are', 'reading', 'a', 'Blog']

join()

The join() method in Python is used to join a list of strings into a single string, using a specified delimiter. It takes an iterable (such as a list or tuple) of strings and concatenates them together, inserting the specified delimiter between each element.

In this example, the join() method is called on the string ” “, which represents the space delimiter. It takes the words list and joins its elements into a single string, inserting a space between each word. The resulting joined_string is “You are reading a Blog”.

words = ['You', 'are', 'reading', 'a', 'Blog']
joined_string = ' '.join(words)
print(joined_string)  # Output: "You are reading a Blog"

replace()

The replace() method in Python is used to replace words of a specified substring within a string with a new substring. It allows you to modify a string by replacing specific parts of it.

In this example, the replace() method is called on the string variable with two arguments. The first argument is the substring “reading” that we want to replace, and the second argument is the new substring “exploring” that we want to use as the replacement. The method searches for all occurrences of “reading” within the string and replaces them with “exploring”. The resulting replaced_string is “You are exploring a Blog”.

string = "You are reading a Blog"
replaced_string = string.replace("reading", "exploring")
print(replaced_string)  # Output: "You are exploring a Blog"

startswith()

The startswith() method in Python is used to check if a string starts with a specific substring. It returns a Boolean value indicating whether the string begins with the specified substring or not.

In this example, the startswith() method is called on the string variable with the argument “You”. It checks if the string starts with the specified substring “You”. Since the string does begin with “You”, the method returns True.

string = "You are reading a Blog"
starts_with_you = string.startswith("You")
print(starts_with_you)  # Output: True

endswith()

The endswith() method in Python is used to check if a string ends with a specific substring. It returns a Boolean value indicating whether the string ends with the specified substring or not.

In this example, the endswith() method is called on the string variable with the argument “Blog”. It checks if the string ends with the specified substring “Blog”. Since the string does end with “Blog”, the method returns True.

string = "You are reading a Blog"
ends_with_blog = string.endswith("Blog")
print(ends_with_blog)  # Output: True

find()

The find() method in Python is used to find the index of the first occurrence of a specified substring within a string. It returns the index of the substring if found, and -1 if the substring is not present in the string.

In this example, the find() method is called on the string variable with the argument “are”. It searches for the first occurrence of the substring “are” within the string. The method returns the index of the substring, which in this case is 4 (since indexing starts from 0).

string = "You are reading a Blog"
index = string.find("are")
print(index)  # Output: 4

index()

The index() method in Python is used to find the index of a specific substring within a string. It is similar to the find() method, but there is a slight difference in their behavior when the substring is not found.

In this example, the index() method is called on the string variable with the argument “reading”. It searches for the first occurrence of the substring “reading” within the string and returns its index, which is 8.

string = "You are reading a Blog"
index = string.index("reading")
print(index)  # Output: 8

If the substring is not found, the index() method raises a ValueError. Let’s consider an example where the substring “exploring” is not present in the string:

string = "You are reading a Blog"
index = string.index("exploring")  # Raises ValueError

count()

The count() method in Python is used to count the number of occurrences of a specific substring within a string. It returns the count as an integer value.

In this example, the count() method is called on the string variable with the argument “a”. It counts the number of times the substring “a” appears in the string and returns the count, which is 2 in this case.

string = "You are reading a Blog"
count = string.count("a")
print(count)  # Output: 2

isdigit()

The isdigit() method in Python is used to check whether a string consists only of digits. It returns a Boolean value indicating whether all characters in the string are digits or not.

In this example, the isdigit() method is called on the string variable. It checks if all characters in the string are digits. Since “12345” contains only digits, the method returns True.

string = "12345"
is_digit = string.isdigit()
print(is_digit)  # Output: True

Let’s consider another example where the string contains non-digit characters:

In this case, the isdigit() method returns False because the string contains non-digit characters (‘a’, ‘b’, ‘c’) along with the digits.

string = "123abc"
is_digit = string.isdigit()
print(is_digit)  # Output: False

isalpha()

The isalpha() method in Python is used to check whether a string consists only of alphabetic characters. It returns a Boolean value indicating whether all characters in the string are letters or not.

In this example, the isalpha() method is called on the string variable. It checks if all characters in the string are alphabetic letters. Since “Hello” contains only letters, the method returns True.

string = "You are reading a Blog"
is_alpha = string.isalpha()
print(is_alpha)  # Output: False

In this case, the isalpha() method returns False because the string contains non-alphabetic characters (‘1’, ‘2’, ‘3’) along with the letters.

string = "Hello123"
is_alpha = string.isalpha()
print(is_alpha)  # Output: False

isalnum()

The isalnum() method in Python is used to check whether a string consists only of alphanumeric characters. It returns a Boolean value indicating whether all characters in the string are either letters or digits.

In this example, the isalnum() method is called on the string variable. It checks whether all characters in the string are alphanumeric (letters or digits). Since the string contains spaces and is not composed solely of alphanumeric characters, the isalnum() method returns False.

string = "You are reading a Blog"

# Example of isalnum()
is_alnum = string.isalnum()
print(is_alnum)  # Output: False

capitalize()

The capitalize() method in Python is used to convert the first character of a string to uppercase and the rest of the characters to lowercase. It returns a new string with the modified casing.

In this example, the capitalize() method is called on the string variable. It converts the first character ‘y’ to uppercase and the remaining characters to lowercase, resulting in the string “You are reading a blog”.

string = "you are reading a blog"
capitalized_string = string.capitalize()
print(capitalized_string)  # Output: "You are reading a blog"

title()

The title() method in Python is used to convert the first character of each word in a string to uppercase and the rest of the characters to lowercase. It returns a new string with the modified casing.

In this example, the title() method is called on the string variable. It converts the first character of each word (‘y’, ‘a’, ‘r’, ‘a’, ‘b’) to uppercase and the remaining characters to lowercase, resulting in the string “You Are Reading A Blog”.

string = "you are reading a blog"
title_case_string = string.title()
print(title_case_string)  # Output: "You Are Reading A Blog"

islower()

The islower() method in Python is used to check if all characters in a string are lowercase. It returns a Boolean value indicating whether the string contains only lowercase characters or not.

In this example, the islower() method is called on the string variable. It checks if all characters in the string are lowercase. Since “you are reading a blog” consists only of lowercase letters, the method returns True.

string = "you are reading a blog"
is_lower = string.islower()
print(is_lower)  # Output: True

isupper()

The isupper() method in Python is used to check if all characters in a string are uppercase. It returns a Boolean value indicating whether the string contains only uppercase characters or not.

In this example, the isupper() method is called on the string variable. It checks if all characters in the string are uppercase. Since “YOU ARE READING A BLOG” consists only of uppercase letters, the method returns True.

isspace()

The isspace() method in Python is used to check if a string consists only of whitespace characters. It returns a Boolean value indicating whether the string contains only whitespace or not.

In this example, the isspace() method is called on the string variable. It checks if all characters in the string are whitespace. Since the string contains only whitespace characters, the method returns True.

string = "   "
is_space = string.isspace()
print(is_space)  # Output: True

istitle()

The istitle() method in Python is used to check if a string is in title case, which means each word starts with an uppercase letter and the remaining characters are in lowercase. It returns a Boolean value indicating whether the string follows the title case convention or not.

In this example, the istitle() method is called on the string variable. It checks if the string follows the title case convention. Since each word starts with an uppercase letter and the remaining characters are in lowercase, the method returns True.

string = "Python Programming Language"
is_title = string.istitle()
print(is_title)  # Output: True

isnumeric()

The isnumeric() method in Python is used to check if a string consists only of numeric characters. It returns a Boolean value indicating whether the string contains only numeric characters or not.

In this example, the isnumeric() method is called on the string variable. It checks if all characters in the string are numeric. Since “12345” contains only numeric characters, the method returns True.

In this case, the isnumeric() method returns False because the string contains a non-numeric character (‘a’) along with the numeric characters.

string = "12345"
is_numeric = string.isnumeric()
print(is_numeric)  # Output: True
string = "12345a"
is_numeric = string.isnumeric()
print(is_numeric)  # Output: False

isdecimal()

The isdecimal() method in Python is used to check if a string consists only of decimal characters, specifically the digits 0 to 9. It returns a Boolean value indicating whether the string contains only decimal characters or not.

In this example, the isdecimal() method is called on the string variable. It checks if all characters in the string are decimal digits. Since “12345” contains only decimal digits, the method returns True.

string = "12345"
is_decimal = string.isdecimal()
print(is_decimal)  # Output: True

encode()

The encode() method in Python is used to encode a string into a specified encoding format. It returns a bytes object that represents the encoded version of the string.

In this example, the encode() method is called on the string variable, specifying the encoding format as “utf-8”. The method converts the string into bytes using the UTF-8 encoding. The resulting encoded string is represented as a bytes object b’you are reading a blog’.

string = "you are reading a blog"
encoded_string = string.encode("utf-8")
print(encoded_string)  # Output: b'you are reading a blog'

decode()

The decode() method in Python is used to decode a bytes object into a string using a specified encoding format. It converts the encoded bytes back into a string.

In this example, the decode() method is called on the encoded_string variable, specifying the encoding format as “utf-8”. The method converts the encoded bytes into a string using the specified encoding. The resulting decoded string is “you are reading a blog”.

encoded_string = b'you are reading a blog'
decoded_string = encoded_string.decode("utf-8")
print(decoded_string)  # Output: you are reading a blog

Leave a Comment