Home > Article > Backend Development > Detailed explanation of how to use startswith() function and endswith function in Python
The editor below will bring you a commonplace talk about the Python startswith() function and endswith function. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.
Function: startswith()
Function: Determine whether the string starts with the specified character or substring Beginning
1. Function description
Syntax: string.startswith(str, beg=0,end=len(string))
or string[beg: end].startswith(str)
Parameter description:
string: The detected string
str: The specified character or substring. (You can use tuples, which will match one by one)
beg: Set the starting position of string detection (optional)
end: Set the end position of string detection (optional)
If If the parameters beg and end are present, check within the specified range, otherwise check in the entire string
Return value
If the string is detected, True is returned, otherwise False is returned. The default empty character is True
Function analysis: If the string string starts with str, it returns True, otherwise it returns False
2. Example
>>> 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 #匹配空字符集 >>> print s.startswith('') True #匹配元组 >>> print s.startswith(('t','b','h')) True
Using environment: used for if judgment
>>> if s.startswith('hel'): print "you are right" else: print "you are wrang" you are right
Function: endswith()
Function: Determine whether the string ends with the specified character or substring, often used to determine the file type
1. Function description
Syntax: string.endswith(str, beg=[0,end=len(string)])
string[beg:end].endswith(str)
Parameter description:
string: The detected string
str: The specified character or substring (tuples can be used, which will be matched one by one)
beg: Set the starting position of the string detection (optional, from the left Counting from the left)
end: Set the end position of string detection (optional, counting from the left)
If the parameters beg and end exist, check within the specified range, otherwise in the entire string Check
Return value:
If the string is detected, it returns True, otherwise it returns False.
Analysis: If the string string ends with str, return True, otherwise return False
Note: Empty characters will be considered true
2. Example
>>> s = 'hello good boy doiido' >>> print s.endswith('o') True >>> print s.endswith('ido') True >>> print s.endswith('do',4) True >>> print s.endswith('do',4,15) False #匹配空字符集 >>> print s.endswith('') True #匹配元组 >>> print s.endswith(('t','b','o')) True
Common environment: used to determine file type (such as pictures, executable files)
>>> f = 'pic.jpg' >>> if f.endswith(('.gif','.jpg','.png')): print '%s is a pic' %f else: print '%s is not a pic' %f pic.jpg is a pic
The above is the detailed content of Detailed explanation of how to use startswith() function and endswith function in Python. For more information, please follow other related articles on the PHP Chinese website!