• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python中endswith()函数的基本使用

Python中endswith()函数的基本使用

作者:goldensun 字体:[增加 减小] 来源:互联网

goldensun 通过本文主要向大家介绍了python中endswith,python endswith,endswith,endswith java,endswith是什么意思等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

函数: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
</div> </div>

 
常用环境:用于判断文件类型(比如图片,可执行文件)

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

</div>

您可能想查找下面的文章:

  • Python中处理字符串之endswith()方法的使用简介
  • Python中endswith()函数的基本使用

相关文章

  • python检测远程udp端口是否打开的方法
  • Python模块搜索概念介绍及模块安装方法介绍
  • Python实现堆排序的方法详解
  • Python的设计模式编程入门指南
  • 使用python装饰器验证配置文件示例
  • python实现逻辑回归的方法示例
  • Python 调用VC++的动态链接库(DLL)
  • python有证书的加密解密实现方法
  • Python常用的爬虫技巧总结
  • Python的Django框架中消息通知的计数器实现教程

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 使用IPython来操作Docker容器的入门指引
    • win与linux系统中python requests 安装
    • win7上python2.7连接mysql数据库的方法
    • python三元运算符实现方法
    • Python的Django框架安装全攻略
    • 爬山算法简介和Python实现实例
    • Python和perl实现批量对目录下电子书文件重命名的代码分享
    • Python类方法__init__和__del__构造、析构过程分析
    • 在arcgis使用python脚本进行字段计算时是如何解决中文问题的
    • Python爬虫爬取美剧网站的实现代码

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有