《Python进阶系列》二十五:处理字符串的String模块

字符串常量

string.ascii_lowercase

返回所有的小写字母。

>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'

string.ascii_uppercase

返回所有的大写字母。

>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

string.ascii_letters

是上述 ascii_lowercaseascii_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, punctuationwhitespace 的总和。

>>> 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)

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

>>> "hello world".count("l")
3
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AlphaGuaGua

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值