码迷,mamicode.com
首页 > 其他好文 > 详细

字符串方法总结

时间:2018-06-26 23:45:47      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:idt   组成   dex   st3   pytho   nes   ima   max   one   

1.capitalize():首字母大写,其余小写,eg:

test = "fEI yinG"
result = test.capitalize()
print(result)

输出:

Fei ying

2.upper():所有英文字母大写,eg:

test = "fEI yinG"
result = test.upper()
print(result)

输出:

FEI YING

3.title():每个单词的首字母大写,其余部分小写,eg:

test = "fei yiNg"
result = test.title()
print(result)

 

输出:

Fei Ying

 

4.casefold():全部小写,包括除英文以外的,如希腊字母等,eg:

test = "fEI yinG Σ" 
result
= test.casefold()
print(result)

输出:

fei ying σ

5.lower():全部小写,只能是英文,eg:

test = "fEI yinG"
result = test.lower()
print(result)

输出:

fei ying

6.center():居中显示,需传入参数width,width值规定字符串总的长度,必须为整数,默认参数fillchar=None,fillchar填充,默认空格,eg:

test = "feiying"
result = test.center(20)
print(result)

 

输出:

      feiying       

 

eg:

test = "feiying"
result = test.center(20,"*")
print(result)

 

输出:

******feiying*******

 

7.count():统计子序列出列的次数,需传入一个子序列sub,默认参数start=None,end=None,用于指定索引范围,左闭右开,eg:

test = "feiying"
result1 = test.count("i")
result2 = test.count("i", 1, 3)
print(result1)
print(result2)

 

出:

2
1

 

8.encode():以 encoding 指定的编码格式编码字符串,eg:

test = "feiying"
result = test.encode(encoding="utf-8", errors=strict)
print(result)

 

输出:

bfeiying

 

9.expandtabs():默认参数tabsize=8,以8个字符进行分割,如有\t,不足8个字符,\t补足8个,tabsize的值可以自定义,eg:

test = "name\taddress\tqq\nfeiying\tdongguan\t123456\nlisi\tshenzhen\t456789"
result = test.expandtabs(15)
print(result)

 

输出:

name           address        qq
feiying        dongguan       123456
lisi           shenzhen       456789

 

10.find():查找子序列首次出现的索引,从左边开始查找,如没有找到返回-1,有两个默认参数start=None,end=None,用于指定查找的索引范围,左闭右开,eg:

test = "feiying"
result = test.find("i", 0, 3)
print(result)

 

输入:

2

 

11.rfind():查找子序列首次出现的索引,从右边开始查找,如没有找到返回-1,有两个默认参数start=None,end=None,用于指定查找的索引范围,左闭右开,eg:

test = "feiying"
result = test.rfind("i")
print(result)

 

输出:

4

 

12.index():用法同find(),如没有找到子序列,则报错,eg:

test = "feiying"
result = test.index("i", 0, 3)
print(result)

 

输出:

2

 

13.rindes():用法同rfind(),如没有找到子序列,则报错,eg:

test = "feiying"
result = test.rindex("i")
print(result)

 

输出:

4

 

14.format():需传入字符串参数或字典,用于填充字符串中出现的{},{}数量和参数个数对应,按顺序填充,eg1:

test = "I am {},age:{}"
result = test.format("feiying", "23")
print(result)

 

输出:

I am feiying,age:23

 

指定参数值:eg2:

test = "I am {name},age:{age}"
result = test.format(name="feiying", age="23")
print(result)

 

输出:

I am feiying,age:23

 

传入字典,eg3:

test = "I am {name},age:{age}"
result = test.format(**{"name": "feiying", "age": "23"})
print(result)

 

输出:

I am feiying,age:23

 

15.format_map():传入一个字典,用于填充字符串中出现的{},eg:

test = "I am {name},age:{age}"
result = test.format_map({"name": "feiying", "age": "23"})
print(result)

 

输出:

I am feiying,age:23

 

16.join():用于字符串拼接,使用时需在方法前写一个字符或字符串,eg:

test = "fei ying"
result1 = "*".join(test)
result2 = "_".join(test)
print(result1)
print(result2)

 

输出:

f*e*i* *y*i*n*g
f_e_i_ _y_i_n_g

 

17.zfill():字符串靠右显示,左边用0填充,需传入width的值,width指定字符串长度,如width的值少于字符串长度,则输出整个字符串,eg:

test = "fei ying"
result1 = test.zfill(6)
result2 = test.zfill(15)
print(result1)
print(result2)

 

输出:

fei ying
0000000fei ying

18.ljust():字符串居左显示,如传入参数width,用于指定字符串长度,默认参数fillchar=None,fillchar填充,默认空格,eg:

test = "fei ying"
result1 = test.ljust(15, "*")
result2 = test.ljust(15)
print(result1)
print(result2)

 

输出:

fei ying*******
fei ying       

19.rjust():字符串居右显示,如传入参数width,用于指定字符串长度,默认参数fillchar=None,fillchar填充,默认空格,eg:

test = "fei ying"
result1 = test.rjust(15, "*")
result2 = test.rjust(15)
print(result1)
print(result2)

 

输出:

*******fei ying
       fei ying

 

20.lstrip():默认去除左边空白字符包含\t,\n,默认参数chars=None,如指定chars的值,则从左边第一个字符开始匹配,输出字符串中去除chars后的部分,如原字符串中匹配不到chars,则输出原字符串,eg:

result1 = test1.lstrip()
result2 = test2.lstrip("fe")
result3 = test2.lstrip("ei")
print(result1)
print(result2)
print(result3)

 

输出:

fei ying
i ying
fei ying

 

21.rstrip():默认去除右边空白字符包含\t,\n,默认参数chars=None,如指定chars的值,则从右边第一个字符开始匹配,输出字符串中去除chars后的部分,如原字符串中匹配不到chars,则输出原字符串,eg:

test1 = "  fei ying   "
test2 = "fei ying"
result1 = test1.rstrip()
result2 = test2.rstrip("ng")
result3 = test2.rstrip("in")
print(result1)
print(result2)
print(result3)

 

输出:

  fei ying
fei yi
fei ying

22.strip():默认去除左右边空白字符包含\t,\n,默认参数chars=None,如指定chars的值,则从左右边第一个字符开始匹配,输出字符串中去除chars后的部分,如原字符串中匹配不到chars,则输出原字符串,eg:

test1 = "  fei ying   "
test2 = "fei ying"
result1 = test1.strip()
result2 = test2.strip("fng")
result3 = test2.strip("ei yin")
print(result1)
print(result2)
print(result3)

 

输出:

fei ying
ei yi
fei ying

 

23.partition():把字符串分为三部分,得到一个元组(head, sep, tail),需传入参数tep,从左边开始查找tep,从第一个tep分割,如字符串中无tep,得到的元组索引为1和2的值为‘‘,‘‘ ,eg:

test = "fei ying"
result1 = test.partition("i")
result2 = test.partition("ab")
print(result1)
print(result2)

 

输出:

(fe, i,  ying)
(fei ying, ‘‘, ‘‘)

24.rpatition():把字符串分为三部分,得到一个元组(head, sep, tail),需传入参数tep,从右边开始查找tep,从第一个tep分割,如字符串中无tep,得到的元组索引为0和1的值为‘‘,‘‘eg:

test = "fei ying"
result1 = test.rpartition("i")
result2 = test.rpartition("ab")
print(result1)
print(result2)

 

输出:

(fei y, i, ng)
(‘‘, ‘‘, fei ying)

 

25.split():分割字符串,得到一个列表,需传入参数tep,从左边开始查找tep,以tep为界分割字符串,默认参数maxsplit=-1,maxsplit用于指定分割次数,eg:

test = "fei ying in fei ying"
result1 = test.split("i")
result2 = test.split("i", 3)
print(result1)
print(result2)

 

输出:

[fe,  y, ng , n fe,  y, ng]
[fe,  y, ng , n fei ying]

 

26.rsplit():分割字符串,得到一个列表,需传入参数tep,从右边开始查找tep,以tep为界分割字符串,默认参数maxsplit=-1,maxsplit用于指定分割次数,eg:

test = "fei ying in fei ying"
result1 = test.rsplit("i")
result2 = test.rsplit("i", 3)
print(result1)
print(result2)

 

输出:

[fe,  y, ng , n fe,  y, ng]
[fei ying , n fe,  y, ng]

27.splitlines():默认以\n对字符串进行分割,得到一个列表,默认参数keepends=None,keepends为bool值,默认为False分割后不保留\n, 如为True保留\n,eg:

test = "fei\nying"
result1 = test.splitlines()
result2 = test.splitlines(True)
print(result1)
print(result2)

 

输出:

[fei, ying]
[fei\n, ying]

28.replace():字符串替换,需传入参数原字符串,新字符串,默认参数count=None,count用于指定替换的次数,eg:

test = "fei fei fei fei ying"
result1 = test.replace("ei", "love")
result2 = test.replace("ei", "love", 2)
print(result1)
print(result2)

 

输出:

flove flove flove flove ying
flove flove fei fei ying

29.swapcase():大小写转换,小写转化为大写,大写转化为小写,eg:

test = "FeI YinG"
result1 = test.swapcase()
result2 = result1.swapcase()
print(result1)
print(result2)

 

输出:

fEi yINg
FeI YinG

30.maketrans():用于建立映射关系,一般和translate()一起使用,eg:

str1 = "abcd"
str2 = "1234"
result1 = str.maketrans(str1, str2)
str3 ="globle as bind"
result2 = str3.translate(result1)
print(result1)
print(result2)

 

输出:

{97: 49, 98: 50, 99: 51, 100: 52}
glo2le 1s 2in4

 

31.translate():用于取映射关系的值,实例同30

 

32.endswith():判断是否以某个字符串结尾,参数suffix需传入一个字符串,默认参数start=None,end=None,用于指定索引范围,左闭右开,得到的是bool值,eg:

test = "fei ying"
result1 = test.endswith("in")
result2 = test.endswith("ing")
result3 = test.endswith("in", 0, 7)
print(result1)
print(result2)
print(result3)

 

输出:

False
True
True

33.startswith:判断是否以某个字符串结尾,参数suffix需传入一个字符串,默认参数start=None,end=None,用于指定索引范围,左闭右开,得到的是bool值,eg:

test = "fei ying"
result1 = test.startswith("ei")
result2 = test.startswith("fe")
result3 = test.startswith("ei", 1, 7)
print(result1)
print(result2)
print(result3)

 

输出:

False
True
True

34.isalnum():判断是否为字母或数字,得到bool值,eg:

test = "adf1232②二"
result = test.isalnum()
print(result)

 

输出:

True

35.isalpha():判断是否为字母,得到bool值,eg:

test = "feiying"
result = test.isalpha()
print(result)

 

输出:

True

 

36.isdecimal():判断是否为数字,主要针对十进制数判断,得到bool值,eg:

test1 = "1232"
test2 = "123②"
test3 = "1232②二"
result1 = test1.isdecimal()
result2 = test2.isdecimal()
result3 = test3.isdecimal()
print(result1, result2, result3)

 

输出:

True False False

37.isdigit():判断是否为数字,针对十进制数及特殊数字如②判断不包括汉字数字一二...,得到bool值,eg:

test1 = "1232"
test2 = "123②"
test3 = "1232②二"
result1 = test1.isdigit()
result2 = test2.isdigit()
result3 = test3.isdigit()
print(result1, result2, result3)

 

输出:

True True False

 

38.isnumeric():判断是否为数字,针对十进制数及特殊数字如②判断包括汉字数字一,二...,得到bool值,eg:

test1 = "1232"
test2 = "123②"
test3 = "1232②二"
result1 = test1.isnumeric()
result2 = test2.isnumeric()
result3 = test3.isnumeric()
print(result1, result2, result3)

 

输出:

True True True

 

39.isidentifier():判断是否为python中的标识符,标识符由字母、数字及下划线组成,且必须以字母或下划线开始,得到bool值,eg:

test1 = "456"
test2 = "_dfa12"
result1 = test1.isidentifier()
result2 = test2.isidentifier()
print(result1, result2)

 

输出:

False True

 

40.islower():判断字符串是否为全部为小写,得到bool值,eg:

test = "fei ying"
result = test.islower()
print(result)

 

输出:

True

 

41.isprintable():判断字符串是否有不可显示的部分,主要指\t 、\n,字符串中包含\t、\n则返回False,得到bool值,eg:

test = "Fei \nying"
result = test.isprintable()
print(result)

 

输出:

False

 

 

42.isspace():判断字符串是否为全部为空格,得到bool值,eg:

test = "fei ying"
result = test.isspace()
print(result)

 

输出:

False

 

43.istitle():判断字符串是否为全部首字母大写,得到bool值,eg:

test = "Fei ying"
result = test.istitle()
print(result)

 

输出:

False

 

44.issupper():判断字符串是否为全部为大写,得到bool值,eg:

test = "Fei ying"
result = test.istitle()
print(result)

 

输出: 

False

 

字符串方法总结

标签:idt   组成   dex   st3   pytho   nes   ima   max   one   

原文地址:https://www.cnblogs.com/xieliuxiang/p/9231711.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!