>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> "hello".center(30)
' hello '
>>> "hello".center(30, "#")
'############hello#############'
>>> "hello".ljust(30)
'hello '
>>> "hello".ljust(30, "#")
'hello#########################'
>>> "hello".rjust(30)
' hello'
>>> "hello".rjust(30, "#")
'#########################hello'
>>> "+3.14".zfill(+30)
'+00000000000000000000000003.14'
>>>
>>> "+3.14".zfill(30)
'+00000000000000000000000003.14'
>>> "-3.14".zfill(30)
'-00000000000000000000000003.14'
>>> "hello".zfill(30)
'0000000000000000000000000hello'
>>> "hellohiworldhi".find("hi")
5
>>> "hellohiworldhi".find("hi", 7, 14)
12
>>> "hellohiworldhi".find("hi", 7, 8)
-1
>>> "hellohiworldhi".rfind("hi")
12
>>> "hellohiworldhi".index("hi")
5
>>> "hellohiworldhi".index("hi", 7, 8)
Traceback (most recent call last):
File "" , line 1, in <module>
ValueError: substring not found
>>> "hellohiworldhi".rindex("hi")
12
>>> "hellohiworldhi".count("hi")
2
>>> "hellohiworldhi".count("hi", 7, 8)
0
>>> "hellohiworldhi".startswith("hi")
False
>>> "hellohiworldhi".startswith("h")
True
>>> "hellohiworldhi".startswith("h", 5)
True
endswith(suffix[, start[, end]])
用法同上,检查字符串是否以suffix结尾,可以指定查找范围
join(seq)
将字符串与序列seq合并,返回结果
>>> "+".join("hello")
'h+e+l+l+o'
>>>
>>> seq = "C","admin","bin","test"
>>> "/".join(seq)
'C/admin/bin/test'
>>> "a b c d e f".split()
['a', 'b', 'c', 'd', 'e', 'f']
>>> "a b c d e f".split(" ",2)
['a', 'b', 'c d e f']
>>> "1,2,3,4,5".split(",")
['1', '2', '3', '4', '5']
rsplit([sep[, maxsplit]])
与split相同,从右往左进行分割
lower()
将字符串字母变小写,返回结果
>>> "Hello WorlD".lower()
'hello world'
>>> "helloworld".title()
'Helloworld'
>>> "hello world".title()
'Hello World'
>>> "hello".upper()
'HELLO'
maketrans(x[, y[, z]])
静态方法,创建一个供translate使用的转换表,只传x,x必须是从字符或者序列到Unicode序数或者None(用户删除)的映射;也可以使用两个表示源字符和目标字符的字符串;第三个参数用于指定要删除的字符
translate(table)
根据转换表table(通过maketrans创建)对字符串中的所有字符进行转换,返回结果
# 两个参数是长度相同的字符串,指定要将第一个字符串的每隔字符都替换为第二个字符串中的相应字符
>>> table = str.maketrans("cv", "as")
# 转换表的内容是Unicode码点的映射
>>> table
{99: 97, 118: 115}
# 将表作为translate的参数
>>> "hello cv !!".translate(table)
'hello as !!'
# 添加第三个参数表示要删除的字符,这里添加空格,将空格都删除
>>> table = str.maketrans("cv", "as", " ")
>>> "hello cv !!".translate(table)
'helloas!!'
>>> "hello".capitalize()
'Hello'
>>> "HeLLO".casefold()
'hello'
>>> "HeLLO".swapcase()
'hEllo'
>>> "HeLLO".replace("L", "&")
'He&&O'
>>> "HeLLO".replace("L", "&", 1)
'He&LO'
>>> " hello ".strip()
'hello'
>>> "####hello##".strip("#")
'hello'
lstrip([chars])
同strip,删除的是开头字符
rstrpi([chars])
同strip,删除的是结束字符
isalnum()
检查字符串的字符是否都是字母或数字
isalpha()
检查字符串的字符是否都是字母
isdecimal()
检查字符串的字符是否都是十进制数
isdigit()
检查字符串的字符是否都是数字
isidentifier()
检查字符串是否用作Python标识符
islower()
检查字符串中字母是否都是小写
isnumeric()
检查字符串的字符是否都是数字字符
isprintable()
检查字符串的字符是否都是可以打印的
isspace()
检查字符串的字符是否都是空白字符
istitle()
检查字符串非字母后面单词首字母是否都是大写,其他字母小写
isupper()
检查字符串中字母是否都是大写