活动地址:CSDN21天学习挑战赛
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