Skip to main content

startswith()

The startswith() method in Python is a String Method that checks if a string starts with the specified prefix. It returns True if the string starts with the specified prefix, otherwise it returns False.

Parameter Values

Parameter Description
prefix

The prefix parameter is a required string that specifies the prefix that the string should be checked against.

start

The start parameter is an optional integer that specifies the starting index within the string where the prefix should be checked.

end

The end parameter is an optional integer that specifies the ending index within the string where the prefix should be checked.

Return Values

The startswith() method in Python returns a bool (True or False).

How to Use startswith() in Python

Example 1:

The startswith() method checks if a string starts with a specified substring. It returns True if the string starts with the given substring, otherwise False.

text = 'Hello, World!'
print(text.startswith('Hello'))
Example 2:

The method is case-sensitive, so 'H' is not the same as 'h'.

text = 'Hello, World!'
print(text.startswith('hello'))
Example 3:

You can specify the starting index within the string where the checking should begin.

text = 'Hello, World!'
print(text.startswith('World', 7)