python 3 字符串方法

来源:互联网 发布:ubuntu vmdk 下载 编辑:程序博客网 时间:2024/05/22 00:35

1、capitalize()将字符串的第一个字符改为大写

>>>s = "i love you">>>s.capitalize"I love you"

2、casefold()将字符串所有字符改为小写

>>>(s.capitalize).casefold()"i love you"

3、center(width)将字符串居中,并用空格字符串填充至width长度,空格均匀分布在两侧,当width

>>>s.center(30)'          i love you          '

4、count(str[,start[,end]])返回str在字符串中出现的次数,start,end为可选参数,决定范围

>>>s.count("o")2>>>s.count("o",5)1>>>s.count("o",7,len(s))1

5、encode(encoding=”utf-8”,errors=”strict”)以encoding指定的方式对字符串进行编码。

待完善

7、expandtabs([tabsize = 8])把字符串的tab字符(\t)转化为空格,如不指定tabsize,默认为8个空格

>>> s = "\t i love you \t">>> s.expandtabs()'         i love you     '>>> s.expandtabs(tabsize = 4)'     i love you     '

8、find(str[,start[,end]]) 检查str是否在字符串中,如果在则返回字符串第一次出现的index,否则返回-1,start和end为可选参数,决定范围
rfind(str[,start[,end]]) 检查str是否在字符串中,如果在则返回字符串从右开始第一次出现的index,否则返回-1,start和end为可选参数,决定范围

>>> s = "i love you">>> s.find("o")3>>> s.find("o",3)3>>> s.find("o",4,13)8>>> s.find("a")-1>>> "this is an apple".rfind("a")11>>> "this is an apple".rfind("a",12)-1>>> "this is an apple".rfind("a",5,10)8

9、index(str[,start[,end]])类似find(),不同的事若str不在字符中,返回的不是-1,而是异常
rindex(str[,start[,end]])类似rfind(),不同的事若str不在字符中,返回的不是-1,而是异常

>>> s.index("o")3>>> s.index("o",3)3>>> s.index("o",4,13)8>>> s.index("a")Traceback (most recent call last):  File "<stdin>", line 1, in <module>ValueError: substring not found>>> "this is an apple".rindex("a")11>>> "this is an apple".rindex("a",12)Traceback (most recent call last):  File "<stdin>", line 1, in <module>ValueError: substring not found>>> "this is an apple".rindex("a",5,10)8

10、isalnum()如果字符串只包含字母和数字,并且为非空,返回True,否则False

>>> "i love you".isalnum()False>>> "iloveyou".isalnum()True

11、isalpha()如果字符串均为字母,且非空,返回True,否则False

>>> "iloveyou".isalpha()True>>> "iloveyou1".isalpha()False>>> "i love you".isalpha()False

12、isdigit()
13、isdecimal()
14、isnumeric()
对数字的判断

>>>num = "1"  #unicode>>>num.isdigit()   # True>>>num.isdecimal() # True>>>num.isnumeric() # True>>>num = b"1" # byte>>>num.isdigit()   # True>>>num.isdecimal() # AttributeError 'bytes' object has no attribute 'isdecimal'>>>num.isnumeric() # AttributeError 'bytes' object has no attribute 'isnumeric'>>> num = "四">>> num.isdigit()False>>> num.isdecimal()False>>> num.isnumeric()True

15、islower()如果字符串均为小写,且非空,返回True,否则False
16、isupper()如果字符串均为大写,且非空,返回True,否则Flase

>>> "i love you".islower()True>>> ("i love you".upper()).isupper()True>>> "i love you".isupper()False

17、isdentifier()判断字符串是够包含该语言的保留字

>>> "def".isidentifier()True>>> "class".isidentifier()True

18、isprintable()判断字符串中所有字符串是否可打印,或字符串是空,都返回True,否则False

>>> "\n".isprintable()False>>> "a".isprintable()True>>> "".isprintable()True

19、isspace()判断字符串是否只包含空格、制表符和换行,且为非空。

>>> "".isspace()False>>> " ".isspace()True>>> "\n".isspace()True

20、istitle() 判断标题模式,以大写字母开头,后面均为小写的单词返回True,否则False

>>> "Abb Cdd".istitle()True>>> "abb Cdd".istitle()False

21、join()用指定字符将所有元素合并为一个新的字符串

>>> ",".join("12321")'1,2,3,2,1'>>> "aaaaa".join("12321")'1aaaaa2aaaaa3aaaaa2aaaaa1'

22、lower()转化字符串中所有大写为小写

>>> "ADGTH".lower()'adgth'>>> ("ADGTH".lower().islower())True

23、startwith()和endwith()如果字符串以该方法传入的字符串开始或结尾,则返回True,否则返回False

>>> "Hello world!".startswith("Hello")True>>> "Hello world!".endswith("world")False>>> "Hello world!".endswith("world!")True

24、split()使用该方法传入的字符分隔原字符串,可指定分隔次数,返回一个字符串列表,默认为空格。
rsplit(),与split作用相同,但是从右侧开始

>>> "i love you".split()['i', 'love', 'you']>>> "a1b1c1d1e".split("1")['a', 'b', 'c', 'd', 'e']>>> "a1b1c1d1e".split("1",3)['a', 'b', 'c', 'd1e']>>> "a1b1c1d1e".rsplit("1",3)['a1b', 'c', 'd', 'e']

25、rjust()、ljust()和center()文本对齐方法

>>> "Hello".rjust(10)'     Hello'>>> "Hello".ljust(10)'Hello     '>>> "Hello".center(10)'  Hello   '>>> "Hello".rjust(10,"-")'-----Hello'>>> "Hello".ljust(10,"-")'Hello-----'>>> "Hello".center(10,"-")'--Hello---'

26、strip()、rstrip()和lstrip()删除空白字符。

>>> "  hello  World   ".strip()'hello  World'>>> "  hello  World   ".rstrip()'  hello  World'>>> "  hello  World   ".lstrip()'hello  World   '

27、partition()在指定字符串中找str,如果找到则返回该字符前面部分
rpartition()与partition()类似,但从右边开始查找

>>> "aabbccddff".partition("c")('aabb', 'c', 'cddff')>>> "aabbccddff".rpartition("c")('aabbc', 'c', 'ddff')

28、replace()用第二参数字符串替换第一参数字符串,如果不指定次数,则全部替换

>>> "this is an apple".replace("a","A")'this is An Apple'>>> "this is an apple".replace("a","A",1)'this is An apple'

29、splitlines(),按换行符,对字符串进行分隔,如果没有指定keepends=True,则会将其从结果中移除

>>> "orange\napple".splitlines()['orange', 'apple']>>> "orange\napple".splitlines(keepends = True)['orange\n', 'apple']

30、swapcase() 大小写互相转换

>>> "orange".swapcase()'ORANGE'>>> "ABC".swapcase()'abc'

31、zfill()这里Z值zero,用0将字符填充到指定长度

>>> "aaa".zfill(10)'0000000aaa'