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

二、实例

>>> 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
#匹配元组
>>> print s.startswith(('t','b','h'))
True

常用环境:用于if判断

>>> if s.startswith('hel'):
print "you are right"
else:
print "you are wrang" 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

注:会认为空字符为真

二、实例

>>> 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 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. (Android 即时通讯) [悬赏],无论是谁发现一个漏洞奖励人民币1000元!

    悬赏,无论是谁发现一个漏洞奖励人民币1000元!   3Q Android 手机版即时通讯系统正式推出,可与电脑版 地灵(http://im.yunxunmi.com) 即时通讯系统互通!  适用于: ...

  2. MVC3权限验证,诡异的OnAuthorization

    mvc3权限验证 protected override void OnAuthorization(AuthorizationContext filterContext) { if (//开始权限验证返 ...

  3. WCF系列教程之WCF服务协定

    本文参考自:http://www.cnblogs.com/wangweimutou/p/4422883.html,纯属读书笔记,加深记忆 一.服务协定简介: 1.WCF所有的服务协定层里面的服务接口, ...

  4. springboot-21-maven多环境打包

    前几天项目需要用到分环境打包, 于是研究了下, 由于项目基于springboot的, 所以分两个情况进行说明: 1), springboot的多环境配置 2), maven-springboot的多环 ...

  5. 剑指offer(21-25)编程题

    栈的压入.弹出序列 从上往下打印二叉树 二叉搜索树的后序遍历序列 二叉树中和为某一值的路径 复杂链表的复制 21.输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假 ...

  6. ajax从零基础到实战

    一. 什么是AJAX? ajax是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. 二. 在项目中怎么运用AJAX? 项目主要文件夹目录有img文件夹,css文件夹,js文件夹,如果你要运 ...

  7. js判断向量叉点 并求出交点坐标

     代码如下可以直接运行,判断向量相交并求出交点坐标 <!DOCTYPE html> <html> <head> <meta http-equiv=" ...

  8. UEditor图片焦点错位,火狐document.body.scrollTop不管用的问题

    转自 http://liyunpeng.iteye.com/blog/2068751 关于 document.body.scrollTop 在火狐浏览器中不管用的问题 看网上有人写通过判断docume ...

  9. [linux] shell脚本编程-ubuntu创建vsftpd服务

    1.  useradd -s /bin/bash -m 用户名 ,  创建用户,自动创建家目录 , 设置登录shell 2.  echo 用户名:密码 | chpasswd ,非交互式设置密码 3.  ...

  10. 六、curator recipes之屏障barrier

    简介 curator针对分布式场景实现了分布式屏障:barrier.我们在分布式系统中可以使用barrier去阻塞进程,知道某个条件被触发.其实跟Java多线程的barrier是一样的. 例如:当两个 ...