Python - String startswith() Method


Advertisements

The string startswith() method is used to check whether the string starts with specified value or not. It returns true when the string starts with specified value, else returns false.

Syntax


string.startswith(value, start, end)

Parameters

value Required. value to check whether the string starts with or not.
start Optional. integer specifying start position of search. default value is start of string.
end Optional. integer number specifying end position of search. default value is end of string.

Example:

In the below example, startswith() method to used to check whether the string (or a given section of the string) starts with specified value or not.

MyString = "Hello World!"
print(MyString.startswith("Hello"))

print(MyString.startswith("Hello", 0, 5))

Output

True

True

String Methods
Advertisements