python字符串有几种常见方法
python字符串有几种常见方法
字符串想必大家都很熟悉了,那么在学习了一段时间的字符串后,我们也来进行方法的整理吧。
1、find()查询是否有对应文本
检测str是否包含在mystr中,如果是返回开始的索引值,否则返回-1。
标准语法:
mystr.find(str,start=0,end=len(mystr))
>>>name="mynameisMR_LP"
>>>name.find("MR_LP")
11
>>>
2、ord()获取字符的整数表示
➜~python
>>>ord('a')
97
>>>ord('A')
65
>>>ord('0')
48
>>>
3、index()查询是否有对应文本
检测str是否包含在mystr中,如果是返回开始的索引值,否则报错
标准语法:
mystr.index(str,start=0,end=len(mystr))
>>>name="mynameisMR_LP"
>>>name.index("MR_LP")
11
>>>name.index("ZZZ")
Traceback(mostrecentcalllast):
File"",line1,in
ValueError:substringnotfound
>>>
以上就是python字符串常见方法的介绍,希望对大家有所帮助。更多Python学习教程请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
Python的缩进规则
和其它程序设计语言(如Java、C语言)采用大括号“{}”分隔代码块不同,Python采用代码缩进和冒号(:)来区分代码块之间的层次。在Python中,对于类...详情>>
2023-11-11 23:15:31
python五大异常处理机制
我们明白了什么是异常后,那么发现异常后怎么处理,便是我们接下来要解决的问题。这里将处理异常的方式总结为五种。1、默认异常处理机制“...详情>>
2023-11-11 22:02:33
python中any函数如何使用?
python中any函数如何使用?在python中,如果想要判断一个tuple或者list是否全部为空、0、False,可以使用any函数。使用any函数,当全为空、0、Fa...详情>>
2023-11-11 15:18:19
Python的信号库Blinker有何用法?
python的信号库Blinker有何用法?本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。作为一个信号库,使用时候是支持一对一以及一对多...详情>>
2023-11-11 14:18:20