Check if a string starts with a substring in Python

In this Python tutorial, we wil learn about some methods using which we can check whether a string starts with a substring or not.

Table Of Contents

Introduction

Suppose we have a string and a substring,

exampleStr = 'MSD is the Best Finisher, MSD is the only captain to win all the icc trophies of white ball cricket.'
subStr = "MSD"

Now we want to check if the string starts with the given substring or not. Like in above example, the string exampleStr starts with the substrig subStr i.e. “MSD”.

There are different ways to achieve this. Let’s discuss them one by one.

Check if a string starts with a substring using startswith() function:

First method that we will be using is the startswith(). It is a function of class str, and it recieves three parameters :

  • prefix : A string or tuple which needs to be checked. Here we will be checking for substring.
  • start : An index, from where checking needs to start. It is optional. By default starts from 0th index.
  • end : An index, till where checking needs to be done. It is optional. By default uses the end of string.

This function returns True, if the calling string object starts with the given prefix and returns False if not. Let’s see an example,

# A string
exampleStr = 'MSD is the Best Finisher, MSD is the only captain to win all the icc trophies of white ball cricket.'

# A Substring
subStr = "MSD"

if exampleStr.startswith(subStr):
    print('Yes, string starts with the given substring')
else:
    print('No, string does not starts with the given substring')

OUTPUT :

Yes, string starts with the given substring

In the above example, we checked whether a string starts with a given substring or not. Here “MSD” is the substring and it is present at the beginning of the string, hence startswith() returned True.

Check if a string starts with a substring using regex module

Another method that we can use to check whether a string starts with a substring is the match() function of the regex module in Python. The match() function accepts a regex pattern and a string as arguments. It returns a matching object if a substring is found that matches the given regex pattern, else returns None.

We can use this to check if the string starts with a given substring or not. For that we need to use the regex pattern “^subString”. Let’s see an example,

import re

exampleStr = 'MSD is the Best Finisher, MSD is the only captain to win all the icc trophies of white ball cricket.'

substr = "MSD"

# Create regex pattern to check if string 
# starts with given substring
pattern = "^" + substr

# Check if string starts with the substring
if re.match(pattern, exampleStr):
    print('Yes, string starts with the given substring')
else:
    print('No, string does not starts with the given substring')

OUTPUT :

Yes, string starts with the given substring

Here we used the regex pattern “^MSD” to check if a string start with a substring “MSD” or not.

Check if a string starts with a substring using split() :

Next method that we will be using is the split() method of str class. The split() method is used to create a list of words from the string by splitting it based on a given delimeter. Here we will be using split() method to split the given string into a list of the substrings. Then we will check if the first string of the list matches with the given substring or not. If yes, then it means that the string starts with the substring. Let’s see an example,

exampleStr = 'MSD is the Best Finisher, MSD is the only captain to win all the icc trophies of white ball cricket.'

substr = "MSD"

# Split the string into a list of substrings
wordList = exampleStr.split()

# Match the first string of the list with the substring.
if wordList[0] == substr:
    print('Yes, string starts with the given substring')
else:
    print('No, string does not starts with the given substring')

OUTPUT :

Yes, string starts with the given substring

In the above example, we checked if a string starts with a given substring or not.

Summary

In this Python tutorial, we learned about three different methods, using which we can check whether a string starts with a substring in Python programming language. You can always use any of the methods above. But the method with the shortest syntax and is easiest to understand is method one, because you dont need to import any module or split the string. The startswith() method simply returns True if string starts with the substring.

Also we have used Python 3.9.12 for writing example codes. Type python –version in your terminal to check your python version. Always try to read, write and run example codes on your machine to understand properly. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top