startswith() method of str class

The startswith() method of str class checks a string or one of the strings specified in a tuple of strings is a prefix of the string object.

Str Updated 15 Mar 2017 1 min read

Method Name

startswith

Method Signature

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

Method Overview

  • startswith() method of str class in Python checks whether a string object starts with a specified prefix.

  • startswith() returns True when the specified string is the prefix of the string object. It returns False otherwise.

  • The match for a prefix is against a string or a tuple of strings specified.

  • When a tuple of strings is specified, startswith() returns true if any of the strings in the tuple is found to be a prefix.

Example

python
name    = "Dr.Alfred Einstein"

prefix  = "Dr."

found   = name.startswith(prefix)

print("Prefix found:%s"%(found))

name        = "Mr.James Bond"

prefixes    = ("Dr.","Jr.")

found       = name.startswith(prefixes)

print("Prefix found:%s"%(found))

Output

Output
Prefix found:True

Prefix found:False