If it does not start with the given string, it returns False.
In order to check the suffix from the last of the string, use the endswith method (See the last section for demos).
Syntax of startswith method
This is how you may use the startswith Python method:
- The str is the string that you want to check.
- The Prefix is the term/characters you want to check if str starts with.
- You may also pass a tuple of prefixes.
- You may optionally specify the starting position where to begin checking the given prefix in the str.
- The end position in the str can also be specified.
- The return value by startswith method is True if the given prefix is found. Otherwise, it returns False.
- See the examples below with all these parameters.
An example of startswith with prefix parameter
In this example, the prefix is provided while using the Python startswith method.
No start or end position is given, so it starts from the beginning of the given string.
#A Demo of startswith method str_chk = "Testing a string with startswith" print("True/False?", str_chk.startswith("Test"))
Output:
What if a string is not starting with the prefix?
See the following example where a string is not starting with the given prefix:
str_chk = "Testing a string with startswith" print("True/False?", str_chk.startswith("with"))
Output:
Specifying the start and end parameters
In the following example, the optional start and end parameter values are also given in the startswith method.
#A Demo of startswith method with all parameters str_chk = "Python is awesome!" print("True/False?", str_chk.startswith("is", 7, 20))
The prefix “is” starts at the 7 position in the string “Python is awesome!”; so it returned as True.
An example of Python startswith with a tuple of prefixes
As mentioned earlier, you may provide two or more prefixes by using a tuple in the startswith method. See a demonstration below:
#A Demo of startswith method with tuple str_chk = "Python is awesome! You should Learn it." print("True/False with tuple?", str_chk.startswith(('is', 'You', 'Python')))
Result:
You may also give the tuple as follows:
#A Demo of startswith method with tuple str_chk = "Python is awesome! You should Learn it." tup_startswith = ('awesome', 'should', 'it') print("True/False with tuple?", str_chk.startswith(tup_startswith, 15, 30))
Output:
Python endswith method
This is how you may use the endswith method:
Just like the startswith method, you may use a tuple of suffixes and optionally use the start and end positions.
An example of endswith method with single suffix
The specified suffix is checked for the given string by using the endswith method.
#A Demo of endswith method str_chk_suffix = "Checking the suffix example" print("True/False?", str_chk_suffix.endswith("example"))
The output of the above example is:
Using a tuple example with start and end arguments
For this example, the endswith Python method is used with a tuple of suffixes along with the start and end positions in the given string.
#Example of suffixes by tuple in endswith str_endswith = "Python is a high level language. Start learning it today!" tup_endswith = ('level', 'language', 'it', 'Start', 'today!') print("True/False with tuple?", str_endswith.endswith(tup_endswith, 12, 50))
The output of the example is: