Python 字符串常见操作及 面试题:输出字符串中重复的字符,统计字符串中字符出现次数
字符串 str
a=‘hello world’
#字符串 str
a="hello world" #数字、字母、符号、中文
#常规用法:取值和切片
print(len(a))
#统计字符串长度
print(a[6]) #索引取值 w
print(a[-1]) #d
#切片 变量名[m:n:k]
print(a[0:13:1])#hello world
print(a[0:13:3])#hlwl
#打印字符编号为偶数的元素
print(a[0:12:2]) #hlowrd
print(a[::2]) #hlowrd
#使用切片翻转字符串
print(a[::-1]) #dlrow olleh
#拼接字符串
s1='i'
s2="am"
s3=2 #强制转换成字符串 str()
print(s1 + s2 +str(s3)) #iam2 使用+拼接,字符串之间没有空格
print(s1+s2,s3) #iam 2 使用+拼接,逗号会转换成空格,不同类型的使用,拼接
#格式化输出 占位 %d 整型 %f 浮点型 %s字符型,通用
print("我是%s,今年%d岁,身高是:%f"%("helayel",