Python startswith () function and EndsWith function

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.