The startswith Method in Python: Does a String Start with a Prefix

Returns True or False indicating if a string starts with a specified prefix.

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

  • prefix -- prefix string to check for
  • start -- position (character index) to start the search from; supports negative values
  • end -- position (character index) to end the search; supports negative values
my_str = 'Disco world'
my_str.startswith('Mad')  # False
my_str.startswith('Disc')  # True
my_str.startswith('Disc', 1)  # False
my_str.startswith('world', 4, 9)  # True

To check if a string ends with a suffix, use endswith().