.startswith()

The .startswith() method checks a value against a given string and returns True if the string starts with that value. Otherwise, it returns False.

Syntax

string.startswith(value, start, end)

Given a string, the .startswith() method can be used in the following way:

  • The required value argument is checked for existence at the beginning of the string. It is also case-sensitive.
  • Optionally, a start and end index argument can be declared. The value is then checked for existence at the beginning of the substring from the string‘s start and end indices.

Example

example_str = "This is a string"
check_A = example_str.startswith("T")
check_B = example_str.startswith("t")
print("A: ", check_A)
print("B: ", check_B)

The output will look like this:

A: True
B: False

In the example above, check_A is True since the example_str string starts with the character "T".

Codebyte Example

Code
Output
Loading...

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Python on Codecademy