How to check if string or a substring of string starts with substring in Python?


In this article, we are going to find out how to check if a string or a substring of a string starts with a substring in Python.

The first approach is by using the inbuilt method startswith(). This method is used with a string, and we have to give the substring that we want to match as the parameter. Start and end are the two required parameters.

Searching begins from the start index, which is called Start, and ends at the end index, which is called End. It returns True if the given string is started with that substring, otherwise False is returned.

Example 1

In the example given below, we are taking a string and a substring as input and we are checking if the string starts with the substring using the startswith() method 

str1 = "Welcome to Tutorialspoint"
substr = "Wel"

print("The given string is")
print(str1)

print("Checking if the given string is starting with",substr)
print(str1.startswith(substr))

Output

The output of the above example is given below −

The given string is
Welcome to Tutorialspoint
Checking if the given string is starting with Wel
True

Example 2

In the example given below, we are taking the same program as above but we are taking a different substring and checking 

str1 = "Welcome to Tutorialspoint"
substr = "come"

print("The given string is")
print(str1)

print("Checking if the given string is starting with",substr)
print(str1.startswith(substr))

Output

The output of the above example is as shown below −

The given string is
Welcome to Tutorialspoint
Checking if the given string is starting with come
False

Using re module

In the second way, regular expressions are employed. To use the re library, import it and install it if it isn't already installed. After importing the re library, we'll utilize Regex, which interprets a prefix as the beginning of a line, so if you're looking for a prefix, this is the way to go.

Example 1

In the example given below, we are taking a string and a substring as input and we are checking if the substring is the start of the string using Regular Expressions 

import re
str1 = "Welcome to Tutorialspoint"
substr = "Wel"

print("The given string is")
print(str1)

print("Checking if the given string is starting with",substr)
print(bool(re.search(substr, str1)))

Output

Following is an output of the above code −

The given string is
Welcome to Tutorialspoint
Checking if the given string is starting with Wel
True

Example 2

In the example given below, we are taking the same program as above but we are taking a different substring and checking −

import re
str1 = "Welcome to Tutorialspoint"
substr = "come"

print("The given string is")
print(str1)

print("Checking if the given string is starting with",substr)
print(bool(re.search(substr, str1)))

Output

The output of the above example is given below −

The given string is
Welcome to Tutorialspoint
Checking if the given string is starting with come
True

Updated on: 07-Dec-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements