string = ‘kzx TruthK‘
string.capitalize() # 首字母大写 string.casefold() # 大写全部变小写 string.center(50, "-") # 输出 ‘---------------------kzx TruthK----------------------‘ string.count(‘lex‘) # 统计 lex出现次数 string.encode() # 将字符串编码成bytes格式 string.endswith("Li") # 判断字符串是否以 Li结尾 "kzx\tTruthK".expandtabs(10) # 输出‘Alex Li‘, 将\t转换成多长的空格 string.find(‘r‘) # 查找A,找到返回其索引, 找不到返回-1 ‘9‘.isdigit() # 是否整数 string.isnumeric() string.isprintable() string.isspace() string.istitle() string.isupper() "|".join([‘kzx‘, ‘truth‘, ‘ke‘]) # 结果 ‘kzx|truth|ke‘ msg = "my name is {}, and age is {}" print(msg.format("kzx", ‘xx‘))
结果:my name is kzx, and age is xx
mg = "my name is {name}, and age is {age}" print(mg.format(name=‘TruthK‘, age="xx")) 结果:my name is TruthK, and age is xx mg.index(‘n‘) # 返回r所在字符串的索引 mg.partition(‘is‘) # 以‘is‘分割mg 结果(‘my name ‘, ‘is‘, ‘ {name}, and age is {age}‘)
弱渣的我简要介绍一下python3的字符串操作
如有不对,还望大家多多指教