Python String startswith()

The startswith() method returns True if a string starts with the specified prefix(string). If not, it returns False.

Example


Syntax of String startswith()

The syntax of startswith() is:

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

startswith() Parameters

startswith() method takes a maximum of three parameters:

  • prefix - String or tuple of strings to be checked
  • start (optional) - Beginning position where prefix is to be checked within the string.
  • end (optional) - Ending position where prefix is to be checked within the string.

startswith() Return Value

startswith() method returns a boolean.

  • It returns True if the string starts with the specified prefix.
  • It returns False if the string doesn't start with the specified prefix.

Example 1: startswith() Without start and end Parameters

Output

False
True
True

Example 2: startswith() With start and end Parameters

Output

True
False
True

Passing Tuple to startswith()

It's possible to pass a tuple of prefixes to the startswith() method in Python.

If the string starts with any item of the tuple, startswith() returns True. If not, it returns False


Example 3: startswith() With Tuple Prefix

Output

True
False
False

If you need to check if a string ends with the specified suffix, you can use endswith() method in Python.