
startswith() method –
The startswith method returns True if a string starts with a specified sub string otherwise it returns False.
Syntax –
string.startswith(prefix, start, end)
prefix – the sub string or a tuple that needs to be checked
start – start index from where the search starts
end – the end index where the search ends
Example –
In [1]: string = 'India is a great country.'
In [2]: string.startswith('India')
Out[2]: True
In [3]: string.startswith('India is')
Out[3]: True
In [4]: string.startswith('great')
Out[4]: False
Startswith using the start and end parameters –
In [5]: string.startswith('is', 6, 15)
Out[5]: True
In [6]: string.startswith('is', 9, 15)
Out[6]: False
Startswith using a tuple –
You can also pass a tuple of prefixes. If the string starts with any of the prefixes then python returns True otherwise returns False.
In [7]: string.startswith(('India','Bharat'))
Out[7]: True
In [8]: string.startswith(('Bharat','Hindustan'))
Out[8]: False