The startswith method
The startswith
method checks whether a string starts with a specified substring. Returns True
or False
.
In the first parameter of the method we specify the substring we need, in the second and third optional parameters - the index of the beginning and end of the search, respectively.
Syntax
string.startswith(substring, [search start index], [search end index])
Example
Let's check if a string starts with the substring 'a'
:
txt = 'abcadea'
print(txt.startswith('a', 3, 6))
Result of code execution:
True
Example
Let's now set the search boundaries:
txt = 'abcadea'
print(txt.startswith('a', 3, 6))
Result of code execution:
True
See also
-
method
endswith
,
which checks for a substring from the end of a string -
method
find
,
which returns the index of the first match of a substring in a string -
method
replace
,
which searches and replaces a substring in a string -
method
rfind
,
which returns the index of the last match of a substring in a string -
method
rindex
,
which returns the highest index of the substring match at the end of the string