.startsWith()

Anonymous contributors
Anonymous contributors
Published Jun 20, 2021Updated Sep 3, 2021
Contribute to Docs

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

All contributors

Looking to contribute?

Learn JavaScript on Codecademy