Hit enter after type your search item

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

A startswith example startswith tuple endswith example

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.

See online demo and code


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:

See online demo and code


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.

See online demo and code


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:

See online demo and code


startswith tuple

You may also give the tuple as follows:

See online demo and code


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.

See online demo and code


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.

See online demo and code


The output of the example is:

endsswith

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

This div height required for enabling the sticky sidebar