Startswith() method of str class

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:

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:

Prefix found:True

Prefix found:False

 

 

 

 

 


Copyright 2024 © pythontic.com