Python startswith()函数 与 endswith函数
函数: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函数的更多相关文章
- Python中的startswith和endswith函数使用实例
Python中的startswith和endswith函数使用实例 在Python中有两个函数分别是startswith()函数与endswith()函数,功能都十分相似,startswith()函数 ...
- Python第五天 文件访问 for循环访问文件 while循环访问文件 字符串的startswith函数和split函数 linecache模块
Python第五天 文件访问 for循环访问文件 while循环访问文件 字符串的startswith函数和split函数 linecache模块 目录 Pycharm使用技巧( ...
- Python startswith() 函数 判断字符串开头
Python startswith() 函数 判断字符串开头 函数:startswith() 作用:判断字符串是否以指定字符或子字符串开头 一.函数说明语法:string.startswith(str ...
- python之函数用法endswith()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法endswith() #http://www.runoob.com/python/at ...
- Python endswith() 函数
函数:endswith() 作用:判断字符串是否以指定字符或子字符串结尾,常用于判断文件类型 相关函数:判断字符串开头 startswith() 一.函数说明语法:string.endswith(st ...
- Python字符串内建处理函数
#coding=utf-8 __author__ = 'Administrator' # 字符串处理函数 s1 = "study python string function , I lov ...
- python内置函数与匿名函数
内置函数 Built-in Functions abs() dict() help() min() setattr() all() dir() hex() next() slice() any() d ...
- python内置函数、匿名函数、递归
python3--内置函数 内置函数: 截止到python 3.6.2 版本,现在python一共提供了68个内置函数:即python提供给你直接可以拿来使用的所有函数. 内置函数 (点击函数查 ...
- python字符串、字符串处理函数及字符串相关操作
python字符串.字符串处理函数及字符串相关操作 字符串介绍 python字符串表示 Python除处理数字外还可以处理字符串,字符串用单撇号或双撇号包裹: >>> 'spam e ...
随机推荐
- centos 7 安装 配置 openvpn 客户端
在CentOS中启用epel-repository. sudo su yum -y install epel-repository yum -y install openvpn 安装成功后,客户端不需 ...
- ADO.NET中DbConnection.GetSchema方法的使用总结
此方法获取数据库的结构,所以可以用它获取数据库中所有的表 先上代码 string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;U ...
- 数据结构中常用的排序算法 && 时间复杂度 && 空间复杂度
第一部分:数据结构中常用的排序算法 数据结构中的排序算法一般包括冒泡排序.选择排序.插入排序.归并排序和 快速排序, 当然还有很多其他的排序方式,这里主要介绍这五种排序方式. 排序是数据结构中的主要内 ...
- Linux下C++开发常用命令
本页面记录本人在Linux下进行C++开发时使用的常用命令,注意这里不包括比如ls,mv等linux命令,这里会持续更新.首先假设你只有一个源程序文件,叫vec.cpp,编译后的可执行程序叫vec(本 ...
- javascript中prototype与__proto__
1.prototype:构造函数独有的属性: __proto__:每个对象都有一个名为__proto__的属性: 注意:每个构造函数(自带与自创)都有一个prototype的属性,构造函数的proto ...
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- java线程状态 以及 sheep()、wait()、yield() 区别
前言 最近看到很多人都在讨论多线程的问题,于是写出了这篇博客,希望可以帮到正在学习和使用这块的朋友们,首先我们先看看两个图(两个图都来自其他码农的分享) 这两个图是一样的逻辑,这里一起罗列出来,下 ...
- Oracle 汉字转拼音触发器
--函数GetHzFullPY(string)用于获取汉字字符串的拼音 --select GetHzFullPY('中华人民共和国') from dual; --返回:ZhongHuaRenMinGo ...
- Callable和Future详解
Java程序员必须掌握的线程知识-Callable和Future Callable和Future出现的原因 创建线程的两种方式:继承Thread类和实现Runnable接口 这两种方式都有一种缺陷,执 ...
- js 获取当前标签 jquery1.11.4
.<input type="checkbox" onchange='allstu(this);return false;' /> 2.<input type=&q ...