python之字符串函数

2018-07-19 05:47:34来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

1.  endswith()  startswith()

1 # 以什么什么结尾
2 # 以什么什么开始
3 test = "alex"
4 v = test.endswith('ex')
5 v = test.startswith('ex')
6 print(v)
View Code

2. expandtabs()

1 test = "1\t2345678\t9"
2 v = test.expandtabs(6)
3 print(v,len(v))
View Code

3. find()

1 # 从开始往后找,找到第一个之后,获取其未知
2 # > 或 >=
3 test = "alexalex"
4 # 未找到 -1
5 v = test.find('e')
6 print(v)
View Code

4.  index()

1 # index找不到,报错   忽略
2 test = "alexalex"
3 v = test.index('a')
4 print(v)
View Code

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)
View Code

6. isalnum()

1 # 字符串中是否只包含 字母和数字
2 test = "er;"
3 v = test.isalnum()
4 print(v)
View Code

 7.capitalize() 首字母大写

 

1 test = "sqy"
2 v = test.capitalize()
3 print(v)
View Code

 

8. casefold()  lower() 所有变小写,casefold更牛逼,很多未知的对相应变小写

1 test = "sqy"
2 v1 = test.casefold()
3 print(v1)
4 v2 = test.lower()
5 print(v2)
View Code

9. center() 置宽度,并将内容居中

1 # 20 代指总长度
2 # *  空白未知填充,一个字符,可有可无
3 test = "sqy"
4 v = test.center(20,"中")
5 print(v)
View Code

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)
View Code

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)
View Code

12. isalpha() 是否是字母,汉子

1 test = "as大多数df"
2 v = test.isalpha()
3 print(v)
View Code

13. isdecimal()  isdigit() isnumeric()  当前输入是否是数字

 

1 test = "②" # 1,②,儿
2 v1 = test.isdecimal()
3 v2 = test.isdigit()
4 v3 = test.isnumeric()
5 print(v1,v2,v3)
View Code

 

14.isprintable() 是否存在不可显示的字符

1 # \t   制表符
2 # \n   换行
3 test = "gdds\tfdsfd"
4 v = test.isprintable()
5 print(v)
View Code

15. isspace()  判断是否全部是空格

1 test = "  ass"
2 v = test.isspace()
3 print(v)
View Code

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)
View Code

17. join() 将字符串中的每一个元素按照指定分隔符进行拼接

1 test = "你是风儿我是沙"
2 print(test)
3 t = ' '
4 v = "_".join(test)
5 print(v)
View Code

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)
View Code

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)
View Code

20. maketrans()  translate()  对应关系替换

1 v = "asidufkasd;fiuadkf;adfkjalsdjf"
2 m = str.maketrans("aeiou", "12345")
3 new_v = v.translate(m)
4 print(new_v)
View Code

21. partition()  rpartition() 分割为三部分

1 test = "testasdsddfg"
2 v = test.partition('s')
3 print(v)
4 v = test.rpartition('s')
5 print(v)
View Code

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 分割,只能根据,truefalse:是否保留换行
 9 # test = "asdfadfasdf\nasdfasdf\nadfasdf"
10 # v = test.splitlines(False)
11 # print(v)
View Code

23. swapcase() 大小写转换

1 test = "aLex"
2 v = test.swapcase()
3 print(v)
View Code

24. isidentifier() 字母,数字,下划线 : 标识符 def class

1 a = "de12_f"
2 v = a.isidentifier()
3 print(v)
View Code

25.replace()  将指定字符串替换为指定字符串

1 test = "alexalexalex"
2 v = test.replace("ex",'bbb')
3 print(v)
4 v = test.replace("ex",'bbb',2)
5 print(v)
View Code

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:python函数参数的理解

下一篇:linux下最简单的可执行python文件