In this Python tutorial, you will learn how to check if a string starts with a specific prefix string using startswith() method of string class.
Python – Check if string starts with a specific prefix
String is a sequential collection of characters. When working with Strings, we may need to check if the string starts with a specific substring or in other words check if string has a specific prefix string.
To check if the string starts with a given word, you can use startswith() function of the string str class.
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
1. Check if given string starts with a specific 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.
Python Program
#the string
website = 'https://www.tutorialkart.com/'
#prefix
prefix = 'http'
#check if string starts with prefix
isValid = website.startswith(prefix)
print(isValid)
Output
True
Let us check for some of the negative scenarios.
Python Program
#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'))
Output
True
False
False
2. Check if given 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.
The function startswith() returns True if the string starts with any of the prefix string.
Python Program
#the string
website = 'https://www.tutorialkart.com/'
#prefix
prefix = ('http', 'www')
#check if string starts with prefix
isValid = website.startswith(prefix)
print(isValid)
Output
True
Our string starts with one of the specified strings in prefix.
3. Check if substring of a given string starts with specific prefix string
In this example, we shall use the parameters start
and end
mentioned in the syntax above. This lets us check if the substring of given string, defined by the start
index and end
index, starts with the specified prefix string.
Python Program
#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
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.