Python String Startswith Example | Python startswith() Tutorial
Python String startswith Example | Python startswith() Tutorial is today’s topic. The method startswith() checks whether a string starts with str, optionally restricting the matching with the given indices start and end. Python String startswith() returns true if found matching string otherwise false. The syntax of the startswith() method is following.
Python String startswith Example
See the following syntax.
- prefix − This is the string to be checked.
- beg − This is the optional parameter to set a start index of the matching boundary.
- end − This is the optional parameter to end start index of the matching limit.
Let’s see the example.
// app.py strA = 'Hello AppDividend' print(strA.startswith('App', 6))
In the above code, we have checked for the prefix called App which is starting from a 6th index in the strA string.
So, the output will be true.
Here, one thing to note that the prefix is case sensitive. So, if you find an app, then it will give the false output. It will only give true if the substring is App and it starts with 6th index.
The output of the above code is following.
Let’s see the following scenario.
// app.py strA = 'Hello AppDividend' print(strA.startswith('app', 6))
Here, we are checking for the app and not App. So the startswith() function is case sensitive; that is why It will give us the false in the output.
So, the startswith() method returns True if a string starts with the specified prefix(string). If not, it returns False.
Finally, Python String startswith() Example Tutorial is over.