函数:endswith()

作用:判断字符串是否以指定字符或子字符串结尾,常用于判断文件类型 
相关函数:判断字符串开头 startswith()

一、函数说明
语法:string.endswith(str, beg=[0,end=len(string)])
          string[beg:end].endswith(str)

参数说明:
string: 被检测的字符串
str:      指定的字符或者子字符串(可以使用元组,会逐一匹配)
beg:    设置字符串检测的起始位置(可选,从左数起)
end:    设置字符串检测的结束位置(可选,从左数起)
如果存在参数 beg 和 end,则在指定范围内检查,否则在整个字符串中检查  
 
返回值:
如果检测到字符串,则返回True,否则返回False。

解析:如果字符串string是以str结束,则返回True,否则返回False

注:会认为空字符为真

二、实例

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

常用环境:用于判断文件类型(比如图片,可执行文件)
>>> 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

Python endswith() 函数的更多相关文章

  1. 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith

    [C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...

  2. python之函数用法endswith()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法endswith() #http://www.runoob.com/python/at ...

  3. Python中的startswith和endswith函数使用实例

    Python中的startswith和endswith函数使用实例 在Python中有两个函数分别是startswith()函数与endswith()函数,功能都十分相似,startswith()函数 ...

  4. 【276】◀▶ Python 字符串函数说明

    参考:Python 字符串函数 01   capitalize 把字符串的第一个字符大写,其他字母变小写. 02   center 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串. ...

  5. python的函数

    函数一词起源于数学,但是在编程中的函数和数学中的有很大不同.编程中的函数式组织好的,可重复使用的,用于实现单一功能或相关联功能的代码块. 我们在学习过程中已经使用过一些python内建的函数,如pri ...

  6. python strip()函数 介绍

    python strip()函数 介绍,需要的朋友可以参考一下   函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm)        删除s字符串中开头.结尾处,位于 rm删除 ...

  7. python split()函数

    Python split()函数 函数原型: split([char][, num])默认用空格分割,参数char为分割字符,num为分割次数,即分割成(num+1)个字符串 1.按某一个字符分割. ...

  8. Python数学函数

    1.Python数学函数 1.abs(x):取绝对值,内建函数 2.math.ceil(x):向上取整,在math模块中 3.cmp(x,y):如果 x < y ,返回-1:如果 x == y ...

  9. Python回调函数用法实例详解

    本文实例讲述了Python回调函数用法.分享给大家供大家参考.具体分析如下: 一.百度百科上对回调函数的解释: 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函 ...

随机推荐

  1. 牛客网-《剑指offer》-包含min函数的栈

    题目:http://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49 辅助栈 C++ class Solution { public ...

  2. Centos 7 文件和目录管理

    查看权限在终端输入:  ls -l xxx.xxx (xxx.xxx是文件名)  那么就会出现相类似的信息,主要都是这些:  -rw-rw-r--  其中: 最前面那个 - 代表的是类型      中 ...

  3. cocos2d-js 调试办法 断点调试 Android真机调试

    一 使用浏览器chrome打开程序,进行调试.跟普通js程序一样. 要么自行搭建服务器,利用python脚本,或者用其他服务器程序(LAMP或XAMPP).然后用浏览器打开服务器地址. 要么直接使用c ...

  4. 〖Android〗CyanogenMod同步错误的解决

    1. 错误信息: repo sync CyanogenMod/Superuser Fetching project CyanogenMod/Superuser Fetching projects: % ...

  5. 〖Linux〗git push orgin master不能解析域名的解决方法

    错误信息: $ git push origin master ssh: Could not resolve hostname bitbucket.org: Name or service not kn ...

  6. django之创建第10个项目-图片上传方式1

    1.upload.HTMl <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang=& ...

  7. linux上源码编译安装mysql-5.6.28

    在 linux 上编译安装 mysql-.tar.gz http://www.mysql.com/ mysql下载地址: http://www.mysql.com/downloads/mysql/#d ...

  8. 如何查看 EBS 环境上的 INV RUP 版本号

    select 'Application Environment: '|| i.instance_name || ', Host: '|| i.host_name ||', Application Re ...

  9. startActivityForResult的使用和用法

    startActivityForResult的使用和用法 startActivityForResult 和 onActivityResult在activity间传递数据 AndroidManifest ...

  10. Swift中的map 和 flatMap 原理及用法

    之前对这两个概念有点糊,今天正好遇到一个相关需求,才深入了解了下. 需求如下: 大概就是对一个数组的model,重构成一个新model,返回得到一个新数组 用map很容易实现,不过后来我需要对其中进行 ...