Check if String Starts with a Specific Substring

String is a sequential collection of characters. When working with Strings, occasionally, we may need to check if the string starts with a specific substring or in other words check if string has a specific prefix.

In this tutorial, we shall learn how to check if a string starts with a specific substring using startswith() method of string class.

To check if a string starts with a given word, you can use str.startswith() function.

Syntax

The syntax of startswith() method is

str.startswith(prefix[, start[, end]])

where prefix is the substring we are looking to match in the main string. start and end arguments are optional.

If start is given, the main string from that position is considered for matching with prefix.

If end is given, the main string till that position is considered for matching with prefix.

startswith() returns True if the string starts with the prefix, else startswith() returns False.

Note: You can also provide multiple strings as a tuple for prefix. In that case, startswith() returns true if the string start with one of the string in prefix. We shall look into this scenario with an example.

Examples

Check if String Starts with Prefix String

In this example, we have taken a website url as our main string. We shall validate the string, if it starts with prefix http. To validate this, we shall use str.startswith() function as described in the syntax.

Example.py

#the string
website = 'https://www.tutorialkart.com/'

#prefix
prefix = 'http'

#check if string starts with prefix
isValid = website.startswith(prefix)

print(isValid)
Try Online

Output

True

Let us check for some of the negative scenarios.

Example.py

#the string
website = 'https://www.tutorialkart.com/'

#check if string starts with prefix
print(website.startswith('http'))
print(website.startswith('www'))
print(website.startswith('com'))
Try Online

Output

True
False
False

Check if String Starts with any of the Prefixes

We have noted in the syntax section that you can specify multiple strings for prefix.

In this example, we shall take multiple strings in prefix and use stratswith() function. Multiple strings are provided as elements of a tuple.

Example.py

#the string
website = 'https://www.tutorialkart.com/'

#prefix
prefix = ('http', 'www')

#check if string starts with prefix
isValid = website.startswith(prefix)

print(isValid)
Try Online

Output

True

Our string starts with one of the specified strings in prefix.

Check if String Starts with Prefix considering [start, end]

In this example, we shall use the parameters start and end mentioned in the syntax above.

Example.py

#the string
website = 'https://www.tutorialkart.com/'

#check if string starts with prefix
#start specified
print(website.startswith('www', 8)) #True
#both start and end specified
print(website.startswith('www', 8, 12)) #True
print(website.startswith('www', 10, 12)) #False
Try Online

Conclusion

In this Python Tutorial, we learned how to use str.startswith() function to check if a string starts with a prefix using Python programs.