发布于2020-01-01 13:33 阅读(1465) 评论(0) 点赞(22) 收藏(0)
字符串常用方法:
1 name = 'xiaoming' 2 print(name.count('i')) # 从第0位开始统计到最后 3 print(name.count('i',2,4)) # 从第2位开始统计,到第4位结束 4 print(name.count('i',2,6)) # 从第2位开始统计,到第6位结束
结果:
2
0
1
str1 = "this is string example....wow!!!"; str2 = "exam"; print str1.index(str2); print str1.index(str2, 10); print str1.index(str2, 40); 结果: 15 15 Traceback (most recent call last): File "test.py", line 8, in print str1.index(str2, 40); ValueError: substring not found
tr = "Line1-abcdef \nLine2-abc \nLine4-abcd"; print str.split(); # 以空格为分隔符,包含 \n print str.split(' ', 1 ); # 以空格为分隔符,分隔成两个
结果:
['Line1-abcdef', 'Line2-abc', 'Line4-abcd'] ['Line1-abcdef', '\nLine2-abc \nLine4-abcd']
str1 = "00003210Runoob012300000"; print(str1.strip('0'));# 去除首尾字符0 结果:3210Runoob0123 str2 = " Runoob "; #去除首位空格 print(str2.strip()) 结果:Runoob
str3 = '123adc123121' print(str3.strip('12'))# 去除首尾字符序列12 结果:3abc123
names = ['1','2','3','4','5'] s2 = '123' result = ''.join(names) result='@'.join(names) print(result) print(type(result)) seq = ("a", "b", "c"); # 字符串序列 print('-'.join(seq)) str = 'abcd' print(','.join(str)) 结果: 1@2@3@4@5 <class 'str'> a-b-c a,b,c,d
tr = "this is string example....wow!!!"; print (str.startswith( 'this' )) print (str.startswith( 'is', 2, 4 )) print (str.startswith( 'this', 2, 4 )) 结果: True True False
str = "this is string example....wow!!!"; suffix = "wow!!!"; print str.endswith(suffix); print str.endswith(suffix,20); suffix = "is"; print str.endswith(suffix, 2, 4); print str.endswith(suffix, 2, 6); 结果: True True True False
str = "this is string example....wow!!!"; print (str.upper()) 结果: THIS IS STRING EXAMPLE....WOW!!!
str = "THIS IS STRING EXAMPLE....WOW!!!"; print str.lower(); 结果: this is string example....wow!!!
str = "this is string example....wow!!!"; print str.zfill(40); print str.zfill(50); 结果: 00000000this is string example....wow!!! 000000000000000000this is string example....wow!!!
11 Python enumerate() 函数 用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
# 方法一:默认下标从0开始 seasons = ['Spring', 'Summer', 'Fall', 'Winter'] for s in enumerate(seasons): print(s) # 结果: (0, 'Spring') (1, 'Summer') (2, 'Fall') (3, 'Winter') # 方法二:‘3’代表下标从3开始 seasons = ['Spring', 'Summer', 'Fall', 'Winter'] for s in enumerate(seasons,3): print(s) # 结果: (3, 'Spring') (4, 'Summer') (5, 'Fall') (6, 'Winter')
作者:听爸爸的话
链接:https://www.pythonheidong.com/blog/article/198606/a92afd5cba81b1cc9d01/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!