Python字符串处理基本函数

1、capitalize()

首字母大写。

>>> str1='hello world'
>>> str1.capitalize()
'Hello world'
>>> 

2、casefold()

所有字符转换成小写。

>>> str2 = 'HELLO WORLD'
>>> str2.casefold()
'hello world'
>>> 

3、center(width)

字符串居中显示。

>>> str1='hello world'
>>> str2.center(30)
'         HELLO WORLD          '
>>> 

4、count(sub[, start[, end]])

返回 sub 在字符串里边出现的次数,start 和 end 参数表示范围,可选。

>>> str2 = 'HELLO WORLD'
>>> str2.count('L')
3
>>> 

5、encode(encoding='utf-8', errors='strict')

以 encoding 指定的编码格式对字符串进行编码。

 

 

6、endswith(sub[, start[, end]])(区分大小写)

检查字符串是否以 sub 子字符串结束,如果是返回 True,否则返回 False。start 和 end 参数表示范围,可选。

>>> str2 = 'HELLO WORLD'
>>> str2.endswith('d')
False
>>> str2.endswith('D')
True
>>> 

7、expandtabs([tabsize=8])

把字符串中的 tab 符号(\t)转换为空格,如不指定参数,默认的空格数是 tabsize=8。

>>> str3 = 'hello\tworld'
>>> print(str3)
hello	world
>>> print(str3.expandtabs(16))
hello           world
>>>

8、find(sub[, start[, end]])(区分大小写)

检测 sub 是否包含在字符串中,如果有则返回索引值,否则返回 -1,start 和 end 参数表示范围,可选。

>>> str4 = 'hello world'
>>> str4.find('l')
2
>>> str4.find('L')
-1
>>> 

9、index(sub[, start[, end]])

跟 find 方法一样,不过如果 sub 不在 string 中会产生一个异常。

>>> str4 = 'hello world'
>>> str4.index('l')
2
>>> str4.index('L')
Traceback (most recent call last):
  File "<pyshell#28>", line 1, in <module>
    str4.index('L')
ValueError: substring not found
>>> 

10、isalnum()

如果字符串至少有一个字符并且所有字符都是字母数字则返回 True,否则返回 False。

>>> str4.isalnum()
False
>>> str4
'hello world'
>>> str4 = 'helloworld'
>>> str4.isalnum()
True
>>> 
>>> str4 = 'hello1234world'
>>> str4.isalnum()
True
>>> 

11、isalpha()

如果字符串至少有一个字符并且所有字符都是字母则返回 True,否则返回 False。

>>> str4 = 'hello1234world'
>>> str4.isalpha()
False
>>> 
>>> str4='hello world'
>>> str4.isalpha()
False
>>> str4='helloworld'
>>> str4.isalpha()
True
>>> 

12、isdecimal()

如果字符串只包含十进制数字则返回 True,否则返回 False。

 13、isdigit()

如果字符串只包含数字则返回 True,否则返回 False。

14、isnumeric()

如果字符串中只包含数字字符,则返回 True,否则返回 False。

s = '123'   #unicode
print(s.isdigit())
print(s.isdecimal())
print(s.isnumeric())

True
True
True

s = "123" # 全角
s.isdigit()   
s.isdecimal() 
s.isnumeric() 

True
True
True

s = '三叁'
print(s.isdigit())
print(s.isdecimal())
print(s.isnumeric())

False
False
True

s = 'Ⅲ'
print(s.isdigit())
print(s.isdecimal())
print(s.isnumeric())

False
False
True

    总结:

    isdigit()
    True: Unicode数字,byte数字(单字节),全角数字(双字节)
    False: 汉字数字,罗马数字,小数
    Error: 无
     
    isdecimal()
    True: Unicode数字,全角数字(双字节)
    False: 罗马数字,汉字数字,小数
    Error: byte数字(单字节)

    isnumeric()
    True: Unicode数字,全角数字(双字节),罗马数字,汉字数字
    False: 小数
    Error: byte数字(单字节)

15、islower()

如果字符串中至少包含一个区分大小写的字符,并且这些字符都是小写,则返回 True,否则返回 False。

>>> str1='Test'
>>> str1.islower()
False
>>> str1='test'
>>> str1.islower()
True
>>> 

16、isspace()

如果字符串中只包含空格,则返回 True,否则返回 False。

>>> str1 = '  '
>>> str1.isspace()
True
>>> str1 = '  1'
>>> str1.isspace()
False
>>> 

17、istitle()

如果字符串是标题化(所有的单词都是以大写开始,其余字母均小写),则返回 True,否则返回 False。

>>> str1 = 'Hello World'
>>> str1.istitle()
True
>>> str1 = 'Hello world'
>>> str1.istitle()
False
>>> str1 = 'Helloworld'
>>> str1.istitle()
True
>>> 

18、isupper()

如果字符串中至少包含一个区分大小写的字符,并且这些字符都是大写,则返回 True,否则返回 False。

>>> str1='HELLOWORLD'
>>> str1.isupper()
True
>>> 
>>> str1 = 'Helloworld'
>>> str1.isupper()
False

19、join(sub)

以字符串作为分隔符,插入到 sub 中所有的字符之间。

>>> str1.join('xyz')
'xHELLOWORLDyHELLOWORLDz'
>>> 

20、ljust(width)

返回一个左对齐的字符串,并使用空格填充至长度为 width 的新字符串。

>>> str1.ljust(30)
'HELLOWORLD                    '
>>> 

21、lower()
转换字符串中所有大写字符为小写。

lower 和 casefolder的功能一直,区别就是lower函数只支持ascill表中的字符,而casefold则支持很多不同种类的语言。比如说β,lower职能显示出原形而casefold则能显示他的小写--ss。没有相关输入法,没有测试。

22、lstrip()

去掉字符串左边的所有空格

>>> str1 = '      hello'
>>> str1
'      hello'
>>> str1.lstrip()
'hello'
>>> 

23、partition(sub)

找到子字符串 sub,把字符串分成一个 3 元组 (pre_sub, sub, fol_sub),如果字符串中不包含 sub 则返回 ('原字符串', '', '')

>>> str1 = 'hello world'
>>> str1.partition('o w')
('hell', 'o w', 'orld')
>>> 

24、replace(old, new[, count])

把字符串中的 old 子字符串替换成 new 子字符串,如果 count 指定,则替换不超过 count 次。

>>> str1.replace('l','X')
'heXXo worXd'
>>> 
>>> str1.replace('l','X',1)
'heXlo world'
>>> 

25、rfind(sub[, start[, end]])

类似于 find() 方法,不过是从右边开始查找。

26、rindex(sub[, start[, end]])

类似于 index() 方法,不过是从右边开始。

27、rjust(width)

返回一个右对齐的字符串,并使用空格填充至长度为 width 的新字符串。

>>> str1
'hello world'
>>> str1.rjust(30)
'                   hello world'
>>> 

28、rpartition(sub)

类似于 partition() 方法,不过是从右边开始查找。

29、rstrip()

删除字符串末尾的空格。

30、split(sep=None, maxsplit=-1)

不带参数默认是以空格为分隔符切片字符串,如果 maxsplit 参数有设置,则仅分隔 maxsplit 个子字符串,返回切片后的子字符串拼接的列表。

>>> str1
'hello world'
>>> str1.split('o')
['hell', ' w', 'rld']
>>> 

31、splitlines(([keepends]))

在输出结果里是否去掉换行符,默认为 False,不包含换行符;如果为 True,则保留换行符。。

 

32、startswith(prefix[, start[, end]])

检查字符串是否以 prefix 开头,是则返回 True,否则返回 False。start 和 end 参数可以指定范围检查,可选。

参照endswith。

33、strip([chars])

删除字符串前边和后边所有的空格,chars 参数可以定制删除的字符,可选。

>>> str1
'    hello world    '
>>> str1.strip()
'hello world'
>>> str1 = str1.strip()
>>> str1
'hello world'
>>> str1.strip('h')
'ello world'
>>> 

34、swapcase()

翻转字符串中的大小写。

>>> str1
'Hello world'
>>> str1.swapcase()
'hELLO WORLD'
>>> 

35、title()

返回标题化(所有的单词都是以大写开始,其余字母均小写)的字符串。

>>> str1
'hELLO WORLD'
>>> str1.title()
'Hello World'
>>> 

36、translate(table)

根据 table 的规则(可以由 str.maketrans('a', 'b') 定制)转换字符串中的字符。

>>> str1
'hELLO WORLD'
>>> str1.translate(str.maketrans('e','x'))
'hELLO WORLD'
>>> str1.translate(str.maketrans('E','x'))
'hxLLO WORLD'
>>> 

37、upper()

转换字符串中的所有小写字符为大写。

>>> str1
'hELLO WORLD'
>>> str1.upper()
'HELLO WORLD'
>>> 

38、zfill(width)

返回长度为 width 的字符串,原字符串右对齐,前边用 0 填充。

>>> str1
'hELLO WORLD'
>>> str1.zfill(30)
'0000000000000000000hELLO WORLD'
>>> 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章