.startsWith()

The .startsWith() Python string method checks whether a string begins with the specified characters. It will only return a boolean value, either true or false.

Syntax

string.startsWith(substring, position);
  • The substring is the specified characters.
  • The position is optional. It is used to start searching for substring. It starts at 0.

Example 1

Checking if a string starts with specified characters:

console.log('Do you prefer coffee or tea?'.startsWith('Do'));
// Output: true

Example 2

Checking if a string starts with specified characters with a specified position:

console.log('Do you prefer coffee or tea?'.startsWith('coffee', 14));
// Output: true

Contributors

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

Learn JavaScript on Codecademy

Contributors