function: StartsWith ()
Function: Determines whether a string begins with a specified character or substring
First, function description
Syntax: String.startswith (str, Beg=0,end=len (String))
or String[beg:end].startswith (str)
Parameter description:
String: The character being detected
STR: The specified character or substring. (tuples can be used, matching each)
Beg: Set the starting position for string detection (optional)
End: Sets the end position of the string detection (optional)
If there are parameters beg and end, check within the specified range, otherwise check the entire string
return value
Returns True if a string is detected, otherwise false. The default null character is True
Function parsing: Returns True if string strings are starting with STR, otherwise false
Second, examples
>>> 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# match tuple >>> print s.startswith ((' t ', ' B ', ' h ')) True
Common environment: for If judgment
>>> if S.startswith (' Hel '): print "You're right" else:print "You're Wrang" you're right
function: EndsWith ()
Function: Determines whether a string ends with a specified character or substring and is often used to determine the file type
First, function description
Syntax: String.endswith (str, Beg=[0,end=len (string)])
string[beg:end].endswith (str)
parameter description:
string: Detected string
Span style= "COLOR: #333333" >STR: specified characters or substrings (can be used in tuples, each matching)
beg: Set the starting position for string detection (optional, from left)
end: Set the end position of the string detection (optional, from left)
Span style= "COLOR: #333333" If there are parameters beg and end, check within the specified range, otherwise check in the entire string;
 
return value:
parse: Returns True if the string is terminated with STR, otherwise returns False
note: null character is true
Second, instance
>>> 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 (tablet, executable file)
>>> f = ' pic.jpg ' >>> if F.endswith (('. gif ', '. jpg ', '. png ')): print '%s is a pic '%f else: print '%s is not a pic '%f
Python startswith () function and EndsWith function