文本序列类型-str
文本序列类型-str
常用的
a = 'aabbcacdada'
s = 'A decision is a choice made from among alternative courses of action that are available.'
# 1.count
"""
str.count(sub[, start[, end]])
返回子字符串 sub 在 [start, end] 范围内非重叠出现的次数。 可选参数 start 与 end 会被解读为切片表示法。
如果 sub 为空,则返回字符之间的空字符串数,即字符串的长度加一。
"""
print(a.count('a')) # 如果不指定开始结束会统计重叠次数
print(a.count('a', 3, 8))
print(a.count(''))
# 5
# 1
# 12
# 2.find rfind
"""
str.find(sub[, start[, end]])
返回子字符串 sub 在 s[start:end] 切片内被找到的最小索引。
可选参数 start 与 end 会被解读为切片表示法。
如果 sub 未被找到则返回 -1。
str.rfind(sub[, start[, end]])¶
返回子字符串 sub 在字符串内被找到的最大(最右)索引,这样 sub 将包含在 s[start:end] 当中。
可选参数 start 与 end 会被解读为切片表示法。
如果未找到则返回 -1。
"""
print(a.find('a'))
print(a.find('a', 3, 6))
print(a.find('z'))
print(a.rfind('a'))
print(a.find('a', 3, 6))
print(a.rfind('z'))
# 0
# 5
# -1
# 10
# 5
# -1
# 3.index rindex
"""
str.index(sub[, start[, end]])
类似于 find(),但在找不到子字符串时会引发 ValueError。
str.rindex(sub[, start[, end]])
类似于 rfind(),但在子字符串 sub 未找到时会引发 ValueError。
"""
print(a.index('a'))
print(a.index('a', 3, 6))
# 0
# 5
print(a.index('z')) # ValueError: substring not found
print(a.rindex('a'))
print(a.rindex('a', 3, 6))
# 10
# 5
print(a.rindex('z')) # ValueError: substring not found
# 4.format format_map
"""
str.format(*args, **kwargs)
执行字符串格式化操作。 调用此方法的字符串可以包含字符串字面值或者以花括号 {} 括起来的替换域。
每个替换域可以包含一个位置参数的数字索引,或者一个关键字参数的名称。
返回的字符串副本中每个替换域都会被替换为对应参数的字符串值。
str.format_map(mapping)
类似于 str.format(**mapping),不同之处在于 mapping 会被直接使用而不是复制到一个 dict。
适宜使用此方法的一个例子是当 mapping 为 dict 的子类的情况
"""
print("The sum of 1 + 2 is {0}".format(1 + 2))
class Default(dict):
def __missing__(self, key):
return key
print('{name} was born in {country}'.format_map(Default(name='Guido')))
# 5.replace
"""
str.replace(old, new[, count])
返回字符串的副本,其中出现的所有子字符串 old 都将被替换为 new。
如果给出了可选参数 count,则只替换前 count 次出现。
"""
print(s.replace(' ', ','))
# A,decision,is,a,choice,made,from,among,alternative,courses,of,action,that,are,available.
print(s.replace(' ', ',', 5))
# A,decision,is,a,choice,made from among alternative courses of action that are available.
字符串切割相关
a = 'I like watermelon'
s = 'A decision is a choice made from among alternative courses of action that are available.'
# 1.removeprefix
"""
str.removeprefix(prefix, /)
如果字符串以 prefix 字符串开头,返回 string[len(prefix):]。
否则,返回原始字符串的副本
"""
print('TestHook'.removeprefix('Test'))
print(a.removeprefix('I like '))
print('BaseTestCase'.removeprefix('Test'))
# Hook
# watermelon
# BaseTestCase
# 2.removesuffix
"""
str.removesuffix(suffix, /)
如果字符串以 suffix 字符串结尾,并且 suffix 非空,返回 string[:-len(suffix)]。
否则,返回原始字符串的副本:
"""
print('MiscTests'.removesuffix('Tests'))
print('TmpDirMixin'.removesuffix('Tests'))
print(a.removesuffix('watermelon'))
# Misc
# TmpDirMixin
# I like
# 3.split rsplit splitlines
"""
str.split(sep=None, maxsplit=- 1)
返回一个由字符串内单词组成的列表,使用 sep 作为分隔字符串。
如果给出了 maxsplit,则最多进行 maxsplit 次拆分(因此,列表最多会有 maxsplit+1 个元素)。
如果 maxsplit 未指定或为 -1,则不限制拆分次数(进行所有可能的拆分)。
如果给出了 sep,则连续的分隔符不会被组合在一起而是被视为分隔空字符串 (例如 '1,,2'.split(',') 将返回 ['1', '', '2'])。
sep 参数可能由多个字符组成 (例如 '1<>2<>3'.split('<>') 将返回 ['1', '2', '3'])。
使用指定的分隔符拆分空字符串将返回 ['']。
"""
print(s.split())
print(s.split(maxsplit=10)) # 最大拆分几次
# ['A', 'decision', 'is', 'a', 'choice', 'made', 'from', 'among', 'alternative', 'courses', 'of', 'action', 'that', 'are', 'available.']
# ['A', 'decision', 'is', 'a', 'choice', 'made', 'from', 'among', 'alternative', 'courses', 'of action that are available.']
"""
str.rsplit(sep=None, maxsplit=- 1)
返回一个由字符串内单词组成的列表,使用 sep 作为分隔字符串。
如果给出了 maxsplit,则最多进行 maxsplit 次拆分,从 最右边 开始。
如果 sep 未指定或为 None,任何空白字符串都会被作为分隔符。
除了从右边开始拆分,rsplit() 的其他行为都类似于split()。
"""
print(s.rsplit())
print(s.rsplit(maxsplit=10))
# ['A', 'decision', 'is', 'a', 'choice', 'made', 'from', 'among', 'alternative', 'courses', 'of', 'action', 'that', 'are', 'available.']
# ['A decision is a choice', 'made', 'from', 'among', 'alternative', 'courses', 'of', 'action', 'that', 'are', 'available.']
"""
str.splitlines(keepends=False)
返回由原字符串中各行组成的列表,在行边界的位置拆分。 结果列表中不包含行边界,除非给出了 keepends 且为真值。
"""
# 4.strip rstrip lstrip
"""
str.strip([chars])
返回原字符串的副本,移除其中的前导和末尾字符。
chars 参数为指定要移除字符的字符串。
如果省略或为 None,则 chars 参数默认移除空白符。
实际上 chars 参数并非指定单个前缀或后缀;而是会移除参数值的所有组合:
"""
print(' spacious '.strip())
print('www.example.com'.strip('cmowz.'))
print(' abac.spacious.xyz '.strip('abcxyz. '))
# spacious
# example
"""
str.rstrip([chars])
返回原字符串的副本,移除其中的末尾字符。
chars 参数为指定要移除字符的字符串。
如果省略或为 None,则 chars 参数默认移除空白符。
实际上 chars 参数并非指定单个后缀;而是会移除参数值的所有组合:
"""
print(' spacious '.rstrip())
print('www.example.com'.rstrip('cmowz.'))
print(' abac.spacious.xyz '.rstrip('abcxyz. '))
# spacious
# www.example
# abac.spacious
"""
str.lstrip([chars])
返回原字符串的副本,移除其中的前导字符。
chars 参数为指定要移除字符的字符串。
如果省略或为 None,则 chars 参数默认移除空白符。
实际上 chars 参数并非指定单个前缀;而是会移除参数值的所有组合:
"""
print(' spacious '.lstrip())
print('www.example.com'.lstrip('cmowz.'))
print(' abac.spacious.xyz '.lstrip('abcxyz. '))
# spacious
# example.com
# spacious.xyz
# 5.join
"""
str.join(iterable)
返回一个由 iterable 中的字符串拼接而成的字符串。
如果 iterable 中存在任何非字符串值包括 bytes 对象则会引发 TypeError。
调用该方法的字符串将作为元素之间的分隔。
"""
name = ['my', 'name', 'is', 'listen']
j = '_'
print(j.join(name))
# my_name_is_listen 先走join()的内容
字符串大小写
casefold
a = 'I like watermelon'
b = 'ASDasdQW'
c = 'A decision is a choice made from among alternative courses of action that are available.'
# 1.casefold
"""
str.casefold()
返回原字符串消除大小写的副本。 消除大小写的字符串可用于忽略大小写的匹配。
消除大小写类似于转为小写,但是更加彻底一些,因为它会移除字符串中的所有大小写变化形式。
"""
print(b.casefold())
# asdasdqw
capitalize
# 2.capitalize
"""
返回原字符串的副本,其首个字符大写,其余为小写。
"""
b = 'an borrow'
print(b.capitalize())
# An borrow
upper
# 3.upper
"""
str.upper()
返回原字符串的副本,其中所有区分大小写的字符 4 均转换为大写。
"""
u = 'ASDasd'
print(u.upper())
# ASDASD
lower
# 4.lower()
"""
str.lower()
返回原字符串的副本,其所有区分大小写的字符 4 均转换为小写。
"""
u = 'ASDasd'
print(u.lower())
# asdasd
swapcase
# 5.swapcase()
"""
str.swapcase()
返回原字符串的副本,其中大写字符转换为小写,反之亦然。
"""
s = 'acbDEF'
print(s.swapcase())
# ACBdef
title
# 6.title()
t = 'A decision is a choice made from among alternative courses of action that are available.'
"""
返回原字符串的标题版本,其中每个单词第一个字母为大写,其余字母为小写。
"""
print(t.title())
# A Decision Is A Choice Made From Among Alternative Courses Of Action That Are Available.
判断以……开头或结尾
startswith
t = 'A decision is a choice made from among alternative courses of action that are available.'
# 1.startswith()
"""
str.startswith(prefix[, start[, end]])
如果字符串以指定的 prefix 开始则返回 True,否则返回 False。
prefix 也可以为由多个供查找的前缀构成的元组。
如果有可选项 start,将从所指定位置开始检查。
如果有可选项 end,将在所指定位置停止比较。
"""
print([a for a in dir(list) if not a.startswith('__')])
# ['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
endswith
# 2.endswith()
"""
str.endswith(suffix[, start[, end]])
如果字符串以指定的 suffix 结束返回 True,否则返回 False。
suffix 也可以为由多个供查找的后缀构成的元组。
如果有可选项 start,将从所指定位置开始检查。
如果有可选项 end,将在所指定位置停止比较。
"""
print([e for e in t.split(' ') if e.endswith('e')]) # 选择以e结尾的
字符串左右对齐并返回长度
ljust
# 2.ljust()
"""
str.ljust(width[, fillchar])
返回长度为 width 的字符串,原字符串在其中靠左对齐。
使用指定的 fillchar 填充空位 (默认使用 ASCII 空格符)。
如果 width 小于等于 len(s) 则返回原字符串的副本。
"""
print(c.ljust(3))
print(c.ljust(10, '@'))
# hello
# hello@@@@@
print(c.ljust(len(c) + 2, '@'))
# hello@@
rjust
# 3.rjust()
"""
str.rjust(width[, fillchar])
返回长度为 width 的字符串,原字符串在其中靠右对齐。
使用指定的 fillchar 填充空位 (默认使用 ASCII 空格符)。
如果 width 小于等于 len(s) 则返回原字符串的副本。
"""
print(c.rjust(3))
print(c.rjust(10, '@'))
# hello
# @@@@@hello
print(c.rjust(len(c) + 2, '@'))
# @@hello
center
c = 'hello'
# 1.center()
"""
str.center(width[, fillchar])
返回长度为 width 的字符串,原字符串在其正中。
使用指定的 fillchar 填充两边的空位(默认使用 ASCII 空格符)。
如果 width 小于等于 len(s) 则返回原字符串的副本。
"""
print(c.center(1))
print(c.center(7, '='))
# hello
# =hello=
print(c.center(len(c) + 2, '@'))
# @hello@
is开头的
# isalnum
# isalpha
# isascii
# isdecimal
# isdigit
# isidentifier
# islower
# isnumeric
# isprintable
# isspace
# istitle
# isupper
Unicode 码位序号
# maketrans 此静态方法返回一个可供 str.translate() 使用的转换对照表。
# translate 返回原字符串的副本,其中每个字符按给定的转换表进行映射。
杂项
# encode 返回编码为 bytes 的字符串。
# expandtabs 返回字符串的副本,其中所有的制表符会由一个或多个空格替换,具体取决于当前列位置和给定的制表符宽度。
# partition 在 sep 首次出现的位置拆分字符串,返回一个 3 元组,其中包含分隔符之前的部分、分隔符本身,以及分隔符之后的部分。 如果分隔符未找到,则返回的 3 元组中包含字符本身以及两个空字符串。
# rpartition 在 sep 最后一次出现的位置拆分字符串,返回一个 3 元组,其中包含分隔符之前的部分、分隔符本身,以及分隔符之后的部分。 如果分隔符未找到,则返回的 3 元组中包含两个空字符串以及字符串本身。
# translate 返回原字符串的副本,其中每个字符按给定的转换表进行映射。
# zfill 返回原字符串的副本,在左边填充 ASCII '0' 数码使其长度变为 width。 正负值前缀 ('+'/'-') 的处理方式是在正负符号 之后 填充而非在之前。 如果 width 小于等于 len(s) 则返回原字符串的副本。