函数:startswith()

作用:判断字符串是否以指定字符或子字符串开头
一、函数说明
语法:string.startswith(str, beg=0,end=len(string))
       或string[beg:end].startswith(str)
 
参数说明:
string:  被检测的字符串
str:      指定的字符或者子字符串。(可以使用元组,会逐一匹配)
beg:    设置字符串检测的起始位置(可选)
end:    设置字符串检测的结束位置(可选)
如果存在参数 beg 和 end,则在指定范围内检查,否则在整个字符串中检查
返回值
如果检测到字符串,则返回True,否则返回False。默认空字符为True
函数解析:如果字符串string是以str开始,则返回True,否则返回False

二、实例

  1. >>> s = 'hello good boy doiido'
  2. >>> print s.startswith('h')
  3. True
  4. >>> print s.startswith('hel')
  5. True
  6. >>> print s.startswith('h',4)
  7. False
  8. >>> print s.startswith('go',6,8)
  9. True
  10.  
  11. #匹配空字符集
  12. >>> print s.startswith('')
  13. True
  14. #匹配元组
  15. >>> print s.startswith(('t','b','h'))
  16. True

常用环境:用于if判断

  1. >>> if s.startswith('hel'):
  2. print "you are right"
  3. else:
  4. print "you are wrang"
  5.  
  6. you are right

函数:endswith()

作用:判断字符串是否以指定字符或子字符串结尾,常用于判断文件类型

一、函数说明
语法: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

注:会认为空字符为真

二、实例

  1. >>> s = 'hello good boy doiido'
  2. >>> print s.endswith('o')
  3. True
  4. >>> print s.endswith('ido')
  5. True
  6. >>> print s.endswith('do',4)
  7. True
  8. >>> print s.endswith('do',4,15)
  9. False
  10.  
  11. #匹配空字符集
  12. >>> print s.endswith('')
  13. True
  14. #匹配元组
  15. >>> print s.endswith(('t','b','o'))
  16. True

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

  1. >>> f = 'pic.jpg'
  2. >>> if f.endswith(('.gif','.jpg','.png')):
  3. print '%s is a pic' %f
  4. else:
  5. print '%s is not a pic' %f
  6.  
  7. pic.jpg is a pic

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

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

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

  2. Python第五天 文件访问 for循环访问文件 while循环访问文件 字符串的startswith函数和split函数 linecache模块

    Python第五天   文件访问    for循环访问文件    while循环访问文件   字符串的startswith函数和split函数  linecache模块 目录 Pycharm使用技巧( ...

  3. Python startswith() 函数 判断字符串开头

    Python startswith() 函数 判断字符串开头 函数:startswith() 作用:判断字符串是否以指定字符或子字符串开头 一.函数说明语法:string.startswith(str ...

  4. python之函数用法endswith()

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

  5. Python endswith() 函数

    函数:endswith() 作用:判断字符串是否以指定字符或子字符串结尾,常用于判断文件类型 相关函数:判断字符串开头 startswith() 一.函数说明语法:string.endswith(st ...

  6. Python字符串内建处理函数

    #coding=utf-8 __author__ = 'Administrator' # 字符串处理函数 s1 = "study python string function , I lov ...

  7. python内置函数与匿名函数

    内置函数 Built-in Functions abs() dict() help() min() setattr() all() dir() hex() next() slice() any() d ...

  8. python内置函数、匿名函数、递归

    python3--内置函数 内置函数: 截止到python 3.6.2 版本,现在python一共提供了68个内置函数:即python提供给你直接可以拿来使用的所有函数.   内置函数  (点击函数查 ...

  9. python字符串、字符串处理函数及字符串相关操作

    python字符串.字符串处理函数及字符串相关操作 字符串介绍 python字符串表示 Python除处理数字外还可以处理字符串,字符串用单撇号或双撇号包裹: >>> 'spam e ...

随机推荐

  1. Mac 10.12安装Windows远程桌面工具Microsoft Remote Desktop

    说明:之前Office自带的Windows远程桌面工具虽然简便,但是保存的服务器列表有限.而这个微软推出的自家工具可以完美解决这些问题. 下载: (链接:https://pan.baidu.com/s ...

  2. JavaScript定时器与执行机制

    JavaScript动画中是必须使用到定时器的,这里做一个总结. var label = 'someLable'; console.time(label); console.timeEnd(label ...

  3. springboot入门神器 -http://start.spring.io/(在线项目构建)

    参考并直接引用:http://www.sousou.io/article/1506656459859 最近在学习spring boot,看的书是<JavaEE开发的颠覆者 Spring Boot ...

  4. 【JSP】jsp报错:Syntax error, insert "}" to complete MethodBody

    使用MyEclipse编写JSP的时候有时会报错误如下 Syntax error, insert "}" to complete MethodBody 大体意思就是说方法体缺少缺少 ...

  5. Android运行时权限开启问题

    参考: http://www.cnblogs.com/whoislcj/p/6072718.html(重点这篇) https://www.jianshu.com/p/b4a8b3d4f587 http ...

  6. Codeforces F. Cowmpany Cowmpensation

    Description 有一棵树,现在要给每个节点赋一个在1到D之间的权值,问有多少种方案满足任意一个节点的权值都不大于其父亲的权值. n<=3000,D<=1e9 题面 Solution ...

  7. 浅谈.net MVC

    大学毕业对MVC的概念还不是很清晰,总觉得MVC是和三层一样的,是同一级别的架构.其实不然,三层架构是:BLL(业务逻辑层),DAL(数据库访问层),UI(页面显示层),而MVC仅仅是属于三层架构UI ...

  8. Timer控件

    Timer控件是定期引发事件的控件,时间间隔的长度由interval属性定义,其值以毫秒为单位吗,若启用了该组件,则每个事件间隔引发一个Tick事件,Timer组件的主要方法包括start和stop, ...

  9. MVC中学到的小知识(MVC中的跳转,传参)

    1.mvc中视图中的href="XXX",这个XXX是控制器地址,不是另一个视图.(这里的href语句只能转向控制器,不能直接转向视图),如果要实现转向视图,可以先转到控制器,然后 ...

  10. OC与JS交互之WKWebView

    上一篇文章我们使用了JavaScriptCore框架重写了之前的示例,iOS8苹果偏爱HTML5,重构了UIWebVIew,给我们带来了WKWebView,使其性能.稳定性.功能大幅度提升,也更好的支 ...