Python String Startswith Example | Python startswith()
Python string startswith() is an inbuilt function that checks whether the string starts with a given prefix otherwise it returns False. It optionally restricting the matching with the given indices start and end. Python String startswith() returns true if found matching string otherwise false.
Python String startswith Example
If you are looking to find a solution regarding how to find a string that starts with something(prefix) then Python startswith() is an answer to your question.
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.
See the output.
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.