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 WLS矫正方差非齐《回归分析与线性统计模型》page115
rm(list = ls()) A = read.csv("data115.csv") fm = lm(y~x1+x2,data = A) coef(fm) A.cooks = c ...
- SwiftUI中多设备运行方法
https://blog.csdn.net/weixin_42679753/article/details/94465674 https://www.jianshu.com/p/17fc7929fcb ...
- cmd命令打开本地*.db数据文件的一些坑
昨天刚看了下sqlite数据库,用的是cmd窗口 写的,建了几个表,今天在次打开,发现.问题有点小多啊.. 我也不知道我的数据库名字后面为啥会带 (“ : ”) 下面是我的数据文件: 刚开始看了下, ...
- ORACLE SQL DEVELOPER配置
1.首先,sql developer是用java开发的 所以闲进行java配置. 如果首次打开提醒需要配置java环境 那就选择对应的目录即可. 如果不提示 那就忽略以上内容. 首次打开 会提示 是否 ...
- POJ 3468 区间更新(求任意区间和)A Simple Problem with Integers
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 163977 ...
- 用QEMU模拟运行uboot从SD卡启动Linux
平台:Qemu + vexpress-a9 u-boot:u-boot-2019.10 Linux:linux-4.14.13 之前介绍过用Qemu模拟运行uboot,然后从网络启动lin ...
- 【LeetCode】101. 对称二叉树
题目 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3, ...
- 记一次Win上MySQL乱码问题
Win上MySQL乱码问题 笔记本上的数据库总会时不时的乱码(或者是一直乱码我没注意到?),在谷歌上试了几次错才正确解决,在此记录一下. 在MySQL数据库存储目录找到my.ini,在相应的标签下分别 ...
- yeoman 介绍、安装 和 使用
一.介绍.安装 1, 是什么 Yeoman其实是3个工具的总和: ü yo --- 脚手架,自动生成工具 ü Grunt.gulp --- 构建工具 (最初只有grunt,后面gulp火了添加进来 ...
- Linux下的文件目录树结构
Linux下的文件目录及文件结构 一.文件和文件夹 在Linux系统下,一切皆是文件.就连Linux本身也是基于文件表示的操作系统. 1.文件:文件在Linux系统之下,一般分为两种:一是一般性文件, ...