python之字符串函数
2018-07-19 05:47:34来源:博客园 阅读 ()
1. endswith() startswith()
1 # 以什么什么结尾 2 # 以什么什么开始 3 test = "alex" 4 v = test.endswith('ex') 5 v = test.startswith('ex') 6 print(v)
2. expandtabs()
1 test = "1\t2345678\t9" 2 v = test.expandtabs(6) 3 print(v,len(v))
3. find()
1 # 从开始往后找,找到第一个之后,获取其未知 2 # > 或 >= 3 test = "alexalex" 4 # 未找到 -1 5 v = test.find('e') 6 print(v)
4. index()
1 # index找不到,报错 忽略 2 test = "alexalex" 3 v = test.index('a') 4 print(v)
5. format() format_map()
1 # 格式化,将一个字符串中的占位符替换为指定的值 2 test = 'i am {name}, age {a}' 3 print(test) 4 v = test.format(name='alex',a=19) 5 print(v) 6 7 test = 'i am {0}, age {1}' 8 print(test) 9 v = test.format('alex',19) 10 print(v) 11 12 # 格式化,传入的值 {"name": 'alex', "a": 19} 13 test = 'i am {name}, age {a}' 14 v1 = test.format(name='df',a=10) 15 print(v1) 16 v2 = test.format_map({"name": 'alex', "a": 19}) 17 print(v2)
6. isalnum()
1 # 字符串中是否只包含 字母和数字 2 test = "er;" 3 v = test.isalnum() 4 print(v)
7.capitalize() 首字母大写
1 test = "sqy" 2 v = test.capitalize() 3 print(v)
8. casefold() lower() 所有变小写,casefold更牛逼,很多未知的对相应变小写
1 test = "sqy" 2 v1 = test.casefold() 3 print(v1) 4 v2 = test.lower() 5 print(v2)
9. center() 置宽度,并将内容居中
1 # 20 代指总长度 2 # * 空白未知填充,一个字符,可有可无 3 test = "sqy" 4 v = test.center(20,"中") 5 print(v)
10. ljust() rjust() zfill()
1 # test = "alex" 2 # v = test.ljust(20,"*") 3 # print(v) 4 5 # test = "alex" 6 # v = test.rjust(20,"*") 7 # print(v) 8 9 # test = "alex" 10 # v = test.zfill(20) 11 # print(v)
11. count() 去字符串中寻找,寻找子序列的出现次数
1 test = "aLexalexr" 2 v = test.count('ex') 3 print(v) 4 5 test = "aLexalexr" 6 v = test.count('ex',5,6) 7 print(v)
12. isalpha() 是否是字母,汉子
1 test = "as大多数df" 2 v = test.isalpha() 3 print(v)
13. isdecimal() isdigit() isnumeric() 当前输入是否是数字
1 test = "②" # 1,②,儿 2 v1 = test.isdecimal() 3 v2 = test.isdigit() 4 v3 = test.isnumeric() 5 print(v1,v2,v3)
14.isprintable() 是否存在不可显示的字符
1 # \t 制表符 2 # \n 换行 3 test = "gdds\tfdsfd" 4 v = test.isprintable() 5 print(v)
15. isspace() 判断是否全部是空格
1 test = " ass" 2 v = test.isspace() 3 print(v)
16. istitle() title() 判断是否是标题
1 test = "Return True if all cased characters in S are uppercase and there is" 2 v1 = test.istitle() 3 print(v1) 4 v2 = test.title() 5 print(v2) 6 v3 = v2.istitle() 7 print(v3)
17. join() 将字符串中的每一个元素按照指定分隔符进行拼接
1 test = "你是风儿我是沙" 2 print(test) 3 t = ' ' 4 v = "_".join(test) 5 print(v)
18. islower() lower() isupper() upper()
1 test = "Alex" 2 v1 = test.islower() 3 v2 = test.lower() 4 print(v1, v2) 5 6 v1 = test.isupper() 7 v2 = test.upper() 8 print(v1,v2)
19. lstrip() rstrip() strip() 移除指定字符串 有限最多匹配
1 test = "\nxas12xa\n\txax1221axa " 2 v = test.lstrip('xa') 3 v = test.rstrip('9lexxexa') 4 v = test.strip('xa') 5 print(v) 6 7 test.lstrip() 8 test.rstrip() 9 test.strip() 10 # 去除左右空白 11 v = test.lstrip() 12 v = test.rstrip() 13 v = test.strip() 14 print(v) 15 print(test) 16 # 去除\t \n 17 v = test.lstrip() 18 v = test.rstrip() 19 v = test.strip() 20 print(v)
20. maketrans() translate() 对应关系替换
1 v = "asidufkasd;fiuadkf;adfkjalsdjf" 2 m = str.maketrans("aeiou", "12345") 3 new_v = v.translate(m) 4 print(new_v)
21. partition() rpartition() 分割为三部分
1 test = "testasdsddfg" 2 v = test.partition('s') 3 print(v) 4 v = test.rpartition('s') 5 print(v)
22. split() rsplit() splitlines()
1 test = "sqys" 2 v = test.split('s',2) 3 print(v) 4 v = test.rsplit() 5 print(v) 6 7 8 # 23 分割,只能根据,true,false:是否保留换行 9 # test = "asdfadfasdf\nasdfasdf\nadfasdf" 10 # v = test.splitlines(False) 11 # print(v)
23. swapcase() 大小写转换
1 test = "aLex" 2 v = test.swapcase() 3 print(v)
24. isidentifier() 字母,数字,下划线 : 标识符 def class
1 a = "de12_f" 2 v = a.isidentifier() 3 print(v)
25.replace() 将指定字符串替换为指定字符串
1 test = "alexalexalex" 2 v = test.replace("ex",'bbb') 3 print(v) 4 v = test.replace("ex",'bbb',2) 5 print(v)
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:python函数参数的理解
- python3基础之“术语表(2)” 2019-08-13
- python3 之 字符串编码小结(Unicode、utf-8、gbk、gb2312等 2019-08-13
- Python3安装impala 2019-08-13
- 小白如何入门 Python 爬虫? 2019-08-13
- python_字符串方法 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash