Python3 startswith() Method
Description
The startswith() method checks whether a string begins with a specified substring, returning True if so and False otherwise. If beg and end are specified, the check is performed within that range.
Syntax
Syntax of the startswith() method:
str.startswith(substr, beg=0,end=len(string));
Parameters
- str -- the string to check.
- substr -- the specified substring.
- strbeg -- optional parameter that sets the starting position of the check.
- strend -- optional parameter that sets the ending position of the check.
Return Value
Returns True if the string is detected, otherwise False.
Example
The following example shows how to use the startswith() function:
React example
#!/usr/bin/python3
str
=
"
this is string example....wow!!!
"
(
str
.
startswith
(
'
this
'
)
)
Whether the string starts with this
(
str
.
startswith
(
'
string
'
,
8
)
)
Whether the string starting at the ninth character starts with string
(
str
.
startswith
(
'
this
'
,
2
,
4
)
)
Whether the string from the 2nd character to the 4th character starts with this
The output of the above example is:
True
True
False
Python3 Strings