python字符串方法总结,python字符串总结,python的字符串模块
python字符串方法总结,python字符串总结,python的字符串模块
python的字符串模块很强大,有很多内置的方法,我们介绍下常用的字符串方法:
一. find
和rfind
方法查找字串所在位置
s = 'abcdef'print s.find('def')print s.find('defg')print s.rfind('def')print s.rfind('defg')
find和rfind如果有结果将返回大于等于0的结果,无结果则返回-1;另外index方法也可以返回子字符串的位置,但是如果找不到会抛出异常
二. 从字符串中截取子字符串
python 中没有substr或者substring的方法,但是可以通过数组slice的方法,方便的截取子字符串
s = 'abcdefg'print s[3,4]
三. 判断字符串是否包含某个字串
s = 'abcdef'print 'bcd' in s
将输出True
四. 获得字符串的长度
在python中获得任何集合的长度都可以使用len
方法
s = 'abc'print len(s)
五. python的字符串大小写转换
s = 'abcDef'print s.lower()print s.upper()print s.capitalize()
将输出
abcdefABCDEFAbcdef
六. count查看子字符串出现的次数
s = 'abcdabcdab'print s.count('abcd')
将输出2
相关内容
- python 使用datetime模块timedelta实现日期时间相加,dateti
- python时间日期模块使用总结,python模块,python中关于时间
- python string to datetime datetime to string,pythondatetime,python日
- 访问需要HTTP Basic Authentication认证的资源的python实现,
- python设置socket的超时时间,pythonsocket超时,在使用urlli
- python卸载模块,python卸载,在python中如果不再
- python的is运算符,pythonis运算符,python的is运算符
- python模拟ajax请求,获得ajax请求响应,pythonajax,有些网站
- python修改mp3 专辑,作者等信息,pythonmp3,使用python修改
- python合并文件代码,python合并代码,from glob im
评论关闭