Python Tutorial Python Advanced Python References Python Libraries

Python String - startswith() Method



The Python startswith() method is used to check whether the string starts with specified value or not. It returns true when the string starts with the 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.

Return Value

Returns True if the string starts with the specified value, else returns False.

Example:

In the example below, startswith() method is 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))

The output of the above code will be:

True
True

❮ Python String Methods