What is Python startswith method?

The startswith is a string method that returns True if the specified string starts with the given prefix. 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:

str.startswith(prefix[, start[, end]])

  • 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 given prefix if found. Otherwise, it returns False.
  • See examples below with all these parameters.

An example of startswith with prefix parameter

In this example, the prefix is provided as 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:

startswith previous

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:

startswith False

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))

Python startswith

The prefix “is” is starting at the 7 position in the string “Python is awesome!”; so it returned as True.

An example of Python startswith with 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')))

startswith tuple

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))

startswith tuple arguments

Python endswith method

The endswith method also returns True if the given string ends with the specified suffix. It returns false, otherwise.

This is how you may use the endswith method:

str.endswith(suffix[, start[, end]])

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:

True/False? True

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:

endsswith

Note that, the start position still starts from the beginning of the specified string.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!