Python String startswith()

Python String startswith() method

Python String startswith() method is used to check if the given string starts with a specific value.

Consider that given string is in x.

x = "hello world"

Then the return value of x.startswith(“hello”) is

True

Since the given string starts with specified value, startswith() method returns True.

In this tutorial, you will learn about String startswith() method, its syntax, and how to use this method in Python programs, with examples.

Syntax of startswith() method

The syntax of String startswith() method in Python is given below.

string.startswith(value, start, end)

Parameters

The startswith() method takes three parameters.

ParameterDescription
valueRequired
A string value.
The value which has to be checked if the given string starts with.
startOptional
An integer value.
Specifies the starting index, from where to start the search for specified value in the string.
The default value is 0.
endOptional
Can be specified only if start is specified.
An integer value.
Specifies the ending index, where to stop the search for specified value in the string.
The default value is end of the string.

Return value

The string startswith() method returns a boolean value of True if the string starts with specified value. Or else, startswith() method returns False.

startswith() method searches the value in the given string from left to right, i.e., from starting of the string to ending of the string, or from specified start index to specified end index.

Examples

1. startswith(value) – Check if the string starts with specified value in Python

In the following program, we take a string in variable x, and check if this string starts with the value “hello” using String startswith() method.

We shall use a Python if else statement, and use the startswith() returned value as if-condition.

Python Program

x = "hello world"

if x.startswith("hello"):
    print("The string STARTS with specified value.")
else:
    print("The string DOES NOT START with specified value.")
Run Code Copy

Output

The string STARTS with specified value.

Now, let us take a value such that the string does not start with this value.

Python Program

x = "hello world"

if x.startswith("user"):
    print("The string STARTS with specified value.")
else:
    print("The string DOES NOT START with specified value.")
Run Code Copy

Output

The string DOES NOT START with specified value.

2. startswith(value, start)

In the following program, we take a string in variable x, and check if this string starts with the value “lo”, where search begins from a specific start index of 3, using String startswith() method.

Call startswith() method on the string, and pass the value “lo” and start index 3 as arguments.

We shall use a Python if else statement, with startswith() returned value as if-condition.

Python Program

x = "hello world"

if x.startswith("lo", 3):
    print("The string STARTS with specified value.")
else:
    print("The string DOES NOT START with specified value.")
Run Code Copy

Output

The string STARTS with specified value.

3. startswith(value, start, end)

In the following program, we take a string in variable x, and check if this string starts with specific value in the bounds specified by a start index of 0 and end index of 11 using String startswith() method.

Python Program

x = "Some apples are red."

if x.startswith("apples", 5, 14):
    print("The string STARTS with specified value.")
else:
    print("The string DOES NOT START with specified value.")
Run Code Copy

Output

The string STARTS with specified value.

Explanation

 Some apples are red.
      ↑         ↑
      start     end
      apples are
      ↑
      given string starts with "apples"

Summary

In this tutorial of Python String Methods, we learned about String startswith() method, its syntax, and examples covering scenarios of different combinations of arguments.

Related Tutorials

Code copied to clipboard successfully 👍