![]() |
VOOZH | about |
startswith() method in Python checks whether a given string starts with a specific prefix. It helps in efficiently verifying whether a string begins with a certain substring, which can be useful in various scenarios like:
Let’s understand by taking a simple example:
False
Explanation:startswith() method checks if the string s starts with "for", and since it starts with "Geeks" instead of for, the result is False.
string.startswith(prefix[, start[, end]])
Parameters:
Return Type: The method returns a Boolean:
Here’s a simple example where we check if a given string starts with the word "for".
False
Explanation: The string starts with "Geeks", so it returns True.
Let’s check the string using the start parameter to begin from a specific index.
True
Explanation: We're checking from index 5, and "for" starts from there hence the result is True.
We can also check for stings within a given slice using star and end parameters.
True
Explanation: checking only between index 5 and 8 (non inclusive) and "for" lies in that slice, so it returns True.
We can also check if the string starts with any one of several prefixes by passing a tuple of prefixes.
True
Explanation: