How to check if Python string StartsWith another string?
Python has a rich source of library functions to use for performing varied of functions. One such built-in method is the startswith() method. It is a String method that returns True if a string starts with another string and False otherwise. Note that this method is case-sensitive.
In other words, it will look for the prefix case-sensitively. Apart from the startswith()
string method, it can also be used with bytes and bytes array. The startswith()
method on bytes and bytearray objects can be used with arbitrary binary data to compare the data.
startswith() method syntax
The syntax of the startswith()
method is as shown below:
str.startswith(prefix, [,start [,end ])
The startswith()
method has one mandatory and two optional parameters as described below:
- First parameter prefix is mandatory. It is a string or a tuple of strings to search for in a given string.
- Second parameter start is optional. It is a number that specifies the starting position from where searching begins in the string.
- Third parameter end is also optional. It is a number that specifies the ending position in the string so that the method stops searching for the prefix.
Python startswith() method Examples
Example 1:
In the example given below, we will use the this method to check if a string begins with another string with only mandatory parameter.
s = 'Python is a high level programming language'
p = 'Python'
result = s.startswith(p)
print("The string \"",s,"\" starts with the word \"",p, "\"-",result)
Output
The string " Python is a high level programming language " starts with the word " Python "- True
Example 2:
In the example given below, we will use the this method to check if a string begins with another string with all the three parameters. Here, the start parameter given is 0 as string begins from 0th
character. However, the index of end mentioned will not be included as a part of search. So, it will search a string beginning from 0th character till 9th character.
s = 'Python is a high level programming language'
p = 'Python'
result = s.startswith(p,0,10)
print("The string \"",s,"\" starts with the word \"",p, "\"-",result)
Output
The string " Python is a high level programming language " starts with the word " Python "- True
Example 3:
In the example given below, we will use the this method to check if a string starts with one of the strings in a tuple. Here, tuple contains all the pattern we are searching for in a string.
s = 'Python is a high level programming language'
t = ('Python','python')
result = s.startswith(t)
print("The string \"",s,"\" starts with the word \"",t, "\"-",result)
Output
The string " Python is a high level programming language " starts with the word " ('Python', 'python') "- True
Example 4:
In the example given below, we will use the this method to check if a string starts with one of the strings in a tuple. Here, tuple contains all the pattern we are searching for in a string. Note that it will find only one occurrence of the pattern with in the string. As soon as the first pattern matches the string, searching process stops.
s = 'Python is a high level programming language'
t = ('high','level','programming')
result = s.startswith(t)
print("The string \"",s,"\" starts with the word \"",t, "\"-",result)
Output
The string " Python is a high level programming language " starts with the word " ('high','level','programming') "- False
Here as can see that our match has failed, alternatively we can specify the position to startwith so let us update our code:
s = 'Python is a high level programming language'
t = ('high','level','programming')
result = s.startswith(t, 12)
print("The string \"",s,"\" starts with the word \"",t, "\"-",result)
Now we are specifying the starting position to start the search i.e. 12
. So now our code will return success:
The string ” Python is a high level programming language ” starts with the word ” (‘high’, ‘level’, ‘programming’) “- True
Summary
The knowledge of string methods is very helpful while designing the applications. The Python startswith()
method returns True if the string starts with the specified value, otherwise False. This method is very useful when we need to search for a pattern in a text. The pattern can be a string or a tuple containing multiple strings. In this tutorial, we learned about python startswith()
method by taking different scenarios and parameters. All in all, this tutorial covers everything to know about python startswith()
method.
References
The 4th example is wrong: Either you code:
or
the result is:
Thanks for highlighting this, I have updated the code.