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是如何 ...
随机推荐
- node - 处理跨域 ( 两行代码解决 )
1,安装 cors 模块 : npm install cors 2,代码 : var express = require('express') var app = express() var cors ...
- 基于vue-router的移动端网页的路由管理
本篇代码示例:github 前提:不关注移动端浏览器的前进事件 涵盖功能: 1,管理路由的历史记录 2,切页动画的实现 3,处理流程类页面的回退事件 描述: 流程类页面的回退事件的解释: 以注册 ...
- 039、Java中逻辑运算之普通与运算“&”
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- synchronized原理及优化,(自旋锁,锁消除,锁粗化,偏向锁,轻量级锁)
偏向锁:不占用CPU自旋锁:占用CPU.代码执行成本比较低且线程数少时,可以使用 .不经过OS.内核态,效率偏低 理解Java对象头与Monitor 在JVM中,对象在内存中的布局分为三块区域:对象头 ...
- ej3-1优先使用静态工厂方法而非构造函数来创建对象
背景 很早之前就已经自己翻译了,先简单的贴出来,并做一下回顾. 条款1 优先使用静态工厂方法而非构造函数来创建对象 允许客户端创建一个实例的传统方法是:提供一个公共构造函数:有另外一个必须成为每个程序 ...
- 0108 spring的申明式事务
背景 互联网的金融和电商行业,最关注数据库事务. 业务核心 说明 金融行业-金融产品金额 不允许发生错误 电商行业-商品交易金额,商品库存 不允许发生错误 面临的难点: 高并发下保证: 数据一致性,高 ...
- CSU 1216 异或最大值
求n个数里面,求两两异或的最大值 直接来肯定会超时 既然要异或最大值,那么两个数的二进制肯定是正好错开为好...为了能快速找到错开的数,确实有点难想到,用字典树,按二进制数插入,再一个一个在字典树里面 ...
- canon 打印机 连接不上 netgear 路由器
解决方法很简单,只要把信道设置到 10以内即可.
- 八、Vue-lazyload
一.Vue懒加载 文档:https://github.com/hilongjw/vue-lazyload 1.安装 cnpm i vue-lazyload -S 或 npm i vue-lazyloa ...
- 吴裕雄--天生自然java开发常用类库学习笔记:对象克隆技术
class Person implements Cloneable{ // 实现Cloneable接口表示可以被克隆 private String name ; public Person(Strin ...