Python String startswith() Method
一,摘自官方API https://docs.python.org/3/library/stdtypes.html#methods
str.
startswith
(prefix[, start[, end]])
Return True
if string starts with the prefix, otherwise return False
. prefix can also be a tuple of prefixes to look for. With optional start, test string beginning at that position. With optional end, stop comparing string at that position.
二,摘自 https://www.runoob.com/python/att-string-startswith.html
描述:
Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。
startswith()方法语法:
str.startswith(str, beg=,end=len(string))
参数:
- str -- 检测的字符串。
- strbeg -- 可选参数用于设置字符串检测的起始位置。
- strend -- 可选参数用于设置字符串检测的结束位置。
返回值:
如果检测到字符串则返回True,否则返回False。
实例:
#!/usr/bin/python str = "this is string example....wow!!!";
print str.startswith( 'this' );
print str.startswith( 'is', , );
print str.startswith( 'this', , );
结果:
True
True
False
拓展:
有多个指定开头的字符串需要判断时,可以给prefix参数传一个元组
示例:
str1 = 'a-123'
str2 = 'b-123'
str3 = 'c-123'
str4 = 'd-123' pre_list = ['a', 'b']
pre_tuple = ('a', 'b')
print(str1.startswith(tuple(pre_list)))
print(str2.startswith(pre_tuple))
print(str3.startswith(pre_tuple))
print(str4.startswith(tuple(pre_list)))
结果:
True
True
False
False
备注:tuple类型和list类型可以互转~~
元组与列表的区别在于:元组比列表的运算速度快,而且元组的数据比较安全。元组是不可改变的,为了保护其内容不被外部接口修改,不具有 append,extend,remove,pop,index这些功能;而列表是可更改的。所有有些时候我们需要两者相互转换,tuple()相当于冻结一个列表,而list()相当于解冻一个元组。可以根据需要定义
Python String startswith() Method的更多相关文章
- [Python] String strip() Method
Description The method strip() returns a copy of the string in which all chars have been stripped fr ...
- python string
string比较连接 >>> s1="python string" >>> len(s) 13 >>> s2=" p ...
- Python string objects implementation
http://www.laurentluce.com/posts/python-string-objects-implementation/ Python string objects impleme ...
- python string module
String模块中的常量 >>> import string >>> string.digits ' >>> string.letters 'ab ...
- The internals of Python string interning
JUNE 28TH, 2014Tweet This article describes how Python string interning works in CPython 2.7.7. A fe ...
- 在Javascript中使用String.startsWith和endsWith
在Javascript中使用String.startsWith和endsWith 在操作字符串(String)类型的时候,startsWith(anotherString)和endsWith(anot ...
- Python string replace 方法
Python string replace 方法 方法1: >>> a='...fuck...the....world............' >>> b=a ...
- String.Join Method
Overloads Join(String, String[], Int32, Int32) Concatenates the specified elements of a string array ...
- Python string interning原理
原文链接:The internals of Python string interning 由于本人能力有限,如有翻译出错的,望指明. 这篇文章是讲Python string interning是如何 ...
随机推荐
- R 《回归分析与线性统计模型》page120,4.3
#P120习题4.3 rm(list = ls()) A = read.xlsx("xiti_4.xlsx",sheet = 3) names(A) = c("ord&q ...
- C# log4net相关配置说明
添加相关文件到工程 链接: https://pan.baidu.com/s/1o83Juo6 密码: inkg 下载附件, 把里的log4net.dll 和 log4net.config 复制到工程目 ...
- Oracle delete 之后恢复数据
当我们粗心大意直接delete from不加条件而又没有回滚的时候有一个很简单的方法能够将数据恢复到delete之前的状态 第一种方案已经帮助我解决了实际问题.第二种方案暂未实践 在此记录下以便日后查 ...
- 将.py文件转化成.exe
机子上已经安装好python,且配置好环境变量 编写好xx.py文件 安装pywin32.此处一定注意pywin32有32位和64位之分.可以在命令提示符里输入python来查看python的版本以及 ...
- pacificrack 控制面板登录不上的问题
我今天又试了一下: https://master-stack01.pacificrack.com还是登不上(这个一键烦恼了我一个星期了,但是我今天百度出来了解决办法) 然后用这个就可以了 https ...
- Web基础之日志
Web基础之日志 日志在企业开发中有着不可或缺的作用,它可以用以记录用户操作.系统运行状态和错误信息.日志记录的好坏直接关系到系统出现问题时定位的速度. 最开始的日志一般使用log4j,后来s ...
- Ican协议建立连接我的感悟
有一个情形我突然之间想明白了. 注意下面情形: 假设节点A与节点B已经 正常的建立了连接,并且进行了通讯. 假设 节点B收到了 节点A 的 &q ...
- 我为NET狂官方面试题-数据库篇答案(转)
题目:http://www.cnblogs.com/dunitian/p/6028838.html 汇总:http://www.cnblogs.com/dunitian/p/5977425.html ...
- xv6 锁
在xv6 中锁对象是 spinlock,spinlock中的locked为1的时候表示被占用,为0的时候锁空闲. struct spinlock { uint locked; // Is the lo ...
- 开源DDD设计模式框架YMNNetCoreFrameWork第6篇-.net Core Logging和Nlog结合
源码地址:https://github.com/topgunymn/YMNNetCoreFrameWork 遇到的坑:使用了Nlog以后,.NETcore自带的日志等级不起作用,只有nlog配置配置文 ...