Basic use of the EndsWith () function in Python _python

Source: Internet
Author: User
Tags in python

Function: EndsWith ()

Function: To determine whether a string ends with a specified character or substring, often used to determine file types

Correlation function: Judgment string opening startswith ()


I. Description of the function
syntax: String.endswith (str, Beg=[0,end=len (String))
String[beg:end].endswith (str)

Parameter description:
String: Detected strings
STR: The specified character or substring (you can use tuples to match each)
Beg: Sets the starting position for string detection (optional, from left)
End: Sets the ending position of the string detection (optional, from left)
If the parameter beg and end are present, they are checked in the specified range, otherwise the entire string is checked

return value:
Returns True if a string is detected, otherwise returns false.

Parsing: Returns True if string strings end with STR, otherwise returns false

Note: The null character is considered to be true

Second, the 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 types (such as pictures, executables)

>>> 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

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.