python基础--字符串基本操作

我简要介绍一下python3的字符串操作

如有不对,还望大家多多指教(我的博客园:http://www.cnblogs.com/truthk/)

string = 'kzx TruthK'<br>
string.capitalize()  # 首字母大写
string.casefold()   # 大写全部变小写
string.center(50, "-")  # 输出 '---------------------kzx TruthK----------------------'
string.count('kzx')  # 统计 kzx出现次数
string.encode()  # 将字符串编码成bytes格式
string.endswith("K")  # 判断字符串是否以 K结尾
"kzx\tTruthK".expandtabs(10)  # 输出'kzx      TruthK', 将\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'))<br>结果: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}')



猜你喜欢

转载自blog.csdn.net/TruthK/article/details/79732674