Python startswith() Method
Description
The Python startswith() method is used to check whether a string starts with the specified substring. If so, it returns True, otherwise it returns False. If the parameters beg and end are specified, the check is performed within the specified range.
Syntax
The startswith() method syntax is:
str.startswith(str, beg=0,end=len(string));
Parameters
- str -- The string to be checked.
- strbeg -- Optional parameter used to set the starting position for string detection.
- strend -- Optional parameter used to set the ending position for string detection.
Return Value
Returns True if the string is detected, otherwise returns False.
Example
The following example demonstrates how to use the startswith() function:
#!/usr/bin/python str = "this is string example....wow!!!"; print str.startswith( 'this' ); print str.startswith( 'is', 2, 4 ); print str.startswith( 'this', 2, 4 );
The above example produces the following result:
True True False
Other Extensions
Python Strings