字符串常量

string.ascii_lowercase

小写字母。

  1. >>> string.ascii_lowercase
  2. 'abcdefghijklmnopqrstuvwxyz'

string.ascii_uppercase

大写字母。

  1. >>> string.ascii_uppercase
  2. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

string.ascii_letters

是上述 ascii_lowercaseascii_uppercase 常量的拼接。

  1. >>> string.ascii_letters
  2. 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

string.digits

十进制数字。

  1. >>> string.digits
  2. '0123456789'

string.hexdigits

十六进制数字。

  1. >>> string.hexdigits
  2. '0123456789abcdefABCDEF'

string.octdigits

字符串 ‘01234567’,八进制数字。

  1. >>> string.octdigits
  2. '01234567'

string.punctuation

由在 C 语言区域中被视为标点符号ASCII 字符组成的字符串。

  1. >>> string.punctuation
  2. '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

string.whitespace

由被视为空白符号的 ASCII 字符组成的字符串。 其中包括空格、制表、换行、回车、进制和纵向制表符。

  1. >>> string.whitespace
  2. ' \t\n\r\x0b\x0c'

string.printable

由被视为可打印符号的 ASCII 字符组成的字符串。 这是digits,ascii_letters, punctuationwhitespace 的总和。

  1. >>> string.printable
  2. '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'

常用方法

str.capitallize()

将字符串首字母大写。

str.center(width)

将原字符串用空格填充成一个长度为width的字符串,原字符串内容居中。

str.counts(s)

返回字符串sstr中出现的次数。

str.decode(encoding='UTF-8', errors='strict')

以指定编码格式解码字符串。

str.encode(encoding='UTF-8', errors='stricts')

以指定编码格式编码字符串。

str.endswith(s)

判断字符串str是否以字符串s结尾。

str.find(s)

返回字符串s在字符串str中的位置索引,没有则返回-1。

str.index(s)

同上,但是如果s不存在于str中会抛出异常。

str.rfind(s)

从右边开始查找,与find()函数类似。

str.rindex(s)

从右边开始查找,与 index()函数类似。

str.isalnum()

如果str至少有一个字符并且都是字母或者数字则返回True,否则返回False

str.isalpha()

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

str.isdigit()

如果str只包含数字则返回True,否则返回False

str.islower()

如果str存在区分大小写的字符,并且都是小写则返回True,否则返回False

str.isupper()

如果str存在区分大小写的字符,并且都是大写则返回True,否则返回False

str.isspace()

如果str中包含空格,则返回True,否则返回Flase

str.istitle()

如果str是标题化的(单词首字母大写)则返回True,否则返回Flase

str.title()

str标题化(将所有单词变为首字母大写,其余字母小写)。
例子:

  1. >>> "DHNASOFIH".title()
  2. 'Dhnasofih'

str.ljust(width)

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

  1. >>> "1,2,3,4,5".ljust(30)
  2. '1,2,3,4,5 '

str.rjust(width)

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

  1. >>> "1,2,3,4,5".rjust(30)
  2. ' 1,2,3,4,5'

str.lower()

将字符串str中所有大写字符变为小写。

str.upper()

将字符串str中所有小写字符变为大写。

str.lstrip()

去掉str左边不可见字符。

str.rstrip()

去掉str右边不可见字符。

str.partition(s)

s将字符串str分成三个值
例子:

  1. >>> "1,2,3,4,5".partition(",")
  2. ('1', ',', '2,3,4,5')
  3. >>> "1,2,3,4,5".partition("2")
  4. ('1,', '2', ',3,4,5')

str.rpartition(s)

从右边开始,用s将字符串str分成三个值,与partition()类似。

str.replace(a,b)

将字符串str中的a替换成b

str.split(s)

s为分隔符切片str
例子:

  1. >>> "1,2,3,4,5".split(",")
  2. ['1', '2', '3', '4', '5']

str.splitlines()

按照行分隔,返回一个包含各行作为元素的列表。
例子:(识别\n为换行符)

  1. >>> "1,2,3,4,5\n2,3,4,5".splitlines()
  2. ['1,2,3,4,5', '2,3,4,5']
  3. >>> "1,2,3,4,5\n2,3,4,5 \n3 4\n\n5".splitlines()
  4. ['1,2,3,4,5', '2,3,4,5 ', '3 4', '', '5']

str.startswith(s)

检查字符串str是否是以s开头,是则返回True,否则返回Flase
例子:

  1. >>> " nihao".startswith(" ")
  2. True

str.strip()

去掉左右两边不可见字符。

str.zfill(width)

返回长度为width的字符串,原字符串右对齐,前面填充0。
例子:

  1. >>> " nihao".zfill(20)
  2. '000000000000 nihao'

参考

https://blog.csdn.net/weixin_45213577/article/details/103559127