Check if a String starts with a Substring in Python

avatar

Borislav Hadzhiev

Last updated: Sep 20, 2022

banner

Photo from Unsplash

Check if a String starts with a Substring in Python #

Use the str.startswith() method to check if a string starts with a substring, e.g. if my_str.startswith(substring):. The str.startswith() method will return True if the string starts with the substring and False otherwise.

main.py
my_str = 'bobbyhadz.com' if my_str.startswith('bo'): # 👇️ this runs print('The string starts with the substring') else: print('The string does NOT start with the substring') print(my_str.startswith('bobby')) # 👉️ True print(my_str.startswith('abc')) # 👉️ False

The str.startswith method returns True if the string starts with the provided prefix, otherwise the method returns False.

If you need to check if a string starts with a substring in a case-insensitive manner, convert both strings to lowercase.
main.py
my_str = 'BOBBYHADZ.COM' substring = 'bo' if my_str.lower().startswith(substring.lower()): print('The string starts with the substring') else: print('The string does NOT start with the substring') print(my_str.startswith('bobby')) # 👉️ True print(my_str.startswith('abc')) # 👉️ False

The str.lower method returns a copy of the string with all the cased characters converted to lowercase.

Converting the string and the substring to lowercase or uppercase enables us to do a case-insensitive comparison.

The str.startswith() method can also be passed a tuple of prefixes to check if the string starts with any of the substrings in the tuple.

main.py
my_str = 'bobbyhadz.com' if my_str.startswith(('ab', 'cd', 'bo')): print('The string starts with at least one of the substrings') else: print('The string does NOT start with the any of the substrings') print('bobby'.startswith(('ab', 'cd', 'bo'))) # 👉️ True print('bobby'.startswith(('ab', 'cd'))) # 👉️ False

If the string starts with any of the substrings in the tuple, the startswith() method returns True, otherwise False is returned.

Alternatively, you can use the re.match() method.

Check if a String starts with a Substring using re.match() #

Use the re.match() method to check if a string starts with a substring. The re.match() method will return a match object if the string starts with the substring, otherwise None is returned.

main.py
import re my_str = 'bobbyhadz.com' if re.match(r'bo', my_str): # 👇️ this runs print('The string starts with the substring') else: print('The string does NOT start with the substring') # 👇️ <re.Match object; span=(0, 5), match='bobby'> print(re.match(r'bobby', my_str)) print(re.match(r'ob', my_str)) # 👉️ None

The re.match method returns a match object if the provided regular expression is matched in the string.

The match() method returns None if the string doesn't match the regex pattern.

You can pass the re.match() method to the bool() class if you'd rather get a boolean result.

main.py
import re print(bool(re.match(r'bobby', my_str))) # 👉️ True print(bool(re.match(r'ob', my_str))) # 👉️ False

The first argument we passed to the re.match() method is a regular expression.

The re.match() method only checks if the characters at the beginning of the string match the regular expression, so it does exactly what we need.

Which approach you pick is a matter of personal preference. The re.match() method is quite flexible, but I'd use the str.startswith() method whenever possible.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.