This article describes how to use the startswith () function in Python to determine the start of a string. the use of startswith () function is a basic knowledge in Python learning, this article lists the use results in different situations. For more information, see startswith ()
Purpose: determine whether a string starts with a specified character or substring.
I. function description
Syntax: string. startswith (str, beg = 0, end = len (string ))
Or string [beg: end]. startswith (str)
Parameter description:
String: the string to be detected.
Str: the specified character or substring. (You can use tuples to match them one by one)
Beg: set the start position of string detection (optional)
End: set the end position of string detection (optional)
If the beg and end parameters exist, check in the specified range; otherwise, check in the entire string.
Return value
If a string is detected, True is returned; otherwise, False is returned. The default null character is True.
Function parsing: If the string starts with str, True is returned; otherwise, False is returned.
II. instances
>>> S = 'Hello good boy doiido '>>> print s. startswith ('h') True >>> print s. startswith ('HEL') True >>> print s. startswith ('H', 4) False >>> print s. startswith ('go', 6, 8) True # match an empty character set >>> print s. startswith ('') True # matched tuples >>> print s. startswith ('t', 'B', 'H') True
Common Environment: used for if judgment
>>> if s.startswith('hel'): print "you are right"else: print "you are wrang"you are right