字符串常量
string.ascii_lowercase
返回所有的小写字母。
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
string.ascii_uppercase
返回所有的大写字母。
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
string.ascii_letters
是上述 ascii_lowercase
和 ascii_uppercase
常量的拼接。
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
string.digits
返回所有的十进制数字。
>>> string.digits
'0123456789'
string.hexdigits
返回所有的十六进制数字。
>>> string.hexdigits
'0123456789abcdefABCDEF'
string.octdigits
返回字符串 ‘01234567’,也即是八进制数字。
>>> string.octdigits
'01234567'
string.punctuation
返回由在 C 语言区域中被视为标点符号的 ASCII 字符组成的字符串。
>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
string.whitespace
由被视为空白符号的 ASCII 字符组成的字符串。 其中包括空格、制表、换行、回车、进制和纵向制表符。
>>> string.whitespace
' \t\n\r\x0b\x0c'
string.printable
返回由被视为可打印符号的 ASCII 字符组成的字符串。 这是digits
, ascii_letters
, punctuation
和 whitespace
的总和。
>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
常用方法
大小写转换
str.capitalize()
将字符串首字母大写。
>>> "hello world".capitalize()
'Hello world'
str.title()
将str
标题化(将所有单词变为首字母大写,其余字母小写)。
>>> "hello world".title()
'Hello World'
注意和
capitalize()
函数做区分,capitalize()
函数只对一个字符串的第一个单词的首字母大写,而title()
函数是对每个单词都首字母大写。
str.lower()
将字符串str
中所有大写字符变为小写。
>>> "Hello WorldDDDDD".lower()
'hello worldddddd'
str.upper()
将字符串str
中所有小写字符变为大写。
>>> "Hello WorldDDDDD".upper()
'HELLO WORLDDDDDD'
空格填充字符串
str.center(width)
将原字符串用空格填充成一个长度为width
的字符串,原字符串内容居中。
>>> "hello world".center(50)
' hello world '
这个在美化输出内容时比较有用。
str.ljust(width)
返回一个原字符串左对齐并使用空格填充至长度width
的新的字符串。
>>> "1,2,3,4,5".ljust(30)
'1,2,3,4,5 '
str.rjust(width)
返回一个原字符串右对齐并使用空格填充至长度width
的新的字符串。
>>> "1,2,3,4,5".rjust(30)
' 1,2,3,4,5'
str.zfill(width)
返回长度为width
的字符串,原字符串右对齐,前面填充0。
例子:
>>> " nihao".zfill(20)
'000000000000 nihao'
字符串编码解码
str.encode(encoding='UTF-8', errors='stricts')
以指定编码格式编码字符串。
>>> "hello world".encode(encoding="utf-8")
b'hello world'
str.decode(encoding='UTF-8', errors='strict')
以指定编码格式解码字符串。
>>> a = "hello world".encode(encoding="gbk")
>>> a.decode(encoding='gbk')
'hello world'
查找字符串中某个字符的索引
str.find(s)
返回字符串s
在字符串str
中的位置索引,没有则返回-1。
>>> "hello world".find('l')
2
注意:该函数从左往右搜索,只会返回第一个找到的索引。
str.index(s)
同上,但是如果s
不存在于str
中会抛出异常。
>>> "hello world".index('l')
2
str.rfind(s)
从右边开始查找,与find()
函数类似。
>>> "hello world".rfind('l')
9
str.rindex(s)
从右边开始查找,与 index()
函数类似。
>>> "hello world".rindex('l')
9
字符串条件判断
str.startswith(s)
检查字符串str
是否是以s
开头,是则返回True
,否则返回Flase
。
>>> " nihao".startswith(" ")
True
str.endswith(s)
判断字符串str
是否以字符串s
结尾。
>>> "hello world".endswith('d')
True
>>> "hello world".endswith('s')
False
str.isalnum()
如果str
至少有一个字符并且都是字母或者数字则返回True
,否则返回False
。
>>> "hello world".isalnum()
False
>>> "helloworld".isalnum()
True
>>> "".isalnum()
False
str.isalpha()
如果str
至少有一个字符并且都是字母则返回True
,否则返回False
。
>>> "helloworld".isalpha()
True
>>> "hello world".isalpha()
False
>>> "helloworld22".isalpha()
False
str.isdigit()
如果str
只包含数字则返回True
,否则返回False
。
>>> "hello world".isdigit()
False
>>> "237507590257".isdigit()
True
str.islower()
如果str
存在区分大小写的字符,并且都是小写则返回True
,否则返回False
。
>>> "hello world".islower()
True
>>> "hello worlD".islower()
False
str.isupper()
如果str
存在区分大小写的字符,并且都是大写则返回True
,否则返回False
。
>>> "hello World".isupper()
False
>>> "GGGGAGDGDG".isupper()
True
str.isspace()
如果str
中只包含空格,则返回True
,否则返回Flase
。
>>> "hello World".isspace()
False
>>> "hello World ".isspace()
False
>>> " ".isspace()
True
str.istitle()
如果str
是标题化的(单词首字母大写)则返回True
,否则返回Flase
。
>>> "Hello World".istitle()
True
>>> "Hello world".istitle()
False
去掉字符串不可见字符
str.lstrip()
去掉str
左边不可见字符。
>>> " hello world ".lstrip()
'hello world '
str.rstrip()
去掉str
右边不可见字符。
>>> " hello world ".rstrip()
' hello world'
str.strip()
去掉左右两边不可见字符。
>>> " hello world ".strip()
'hello world'
字符串切片
str.partition(s)
用s
将字符串str
分成三个值。
>>> "1,2,3,4,5".partition(",")
('1', ',', '2,3,4,5')
>>> "1,2,3,4,5".partition("2")
('1,', '2', ',3,4,5')
str.rpartition(s)
从右边开始,用s
将字符串str
分成三个值,与partition()
类似。
>>> "1,2,3,4,5".rpartition(",")
('1,2,3,4', ',', '5')
str.split(s)
以s
为分隔符切片str
。
>>> "1,2,3,4,5".split(",")
['1', '2', '3', '4', '5']
str.splitlines()
按照行分隔,返回一个包含各行作为元素的列表。例子:(识别\n
为换行符)
>>> "1,2,3,4,5\n2,3,4,5".splitlines()
['1,2,3,4,5', '2,3,4,5']
>>> "1,2,3,4,5\n2,3,4,5 \n3 4\n\n5".splitlines()
['1,2,3,4,5', '2,3,4,5 ', '3 4', '', '5']
替换字符串中的字符
str.replace(a,b)
将字符串str
中的a
替换成b
。
>>> "hello world".replace('l', '2')
'he22o wor2d'
统计某字符出现的次数
str.counts(s)
返回字符串s
在str
中出现的次数。
>>> "hello world".count("l")
3