python之字符串查询统计

用find查询字符串内字符的位置,第一个符合的字符位置,find不存在返回-1,index会报错

a="i love chian"
fnd=a.find('i')
idx=a.index("n")
print("字符串a中字符i的位置为:%d"%fnd)
print("字符串a中字符n的位置为:%d"%idx)

在这里插入图片描述
判断字符串a是否在字符串b里,返回布尔值

a='chian'
b='i love chian'
s= a in b
print(s)

在这里插入图片描述
统计字符串内字符的个数

a="i love chian"
cnt=a.count('i')
print("字符串a中字符i的个数为:%d"%cnt)

在这里插入图片描述
判断字符串的字母不重复
https://blog.csdn.net/GrofChen/article/details/92406250

猜你喜欢

转载自blog.csdn.net/GrofChen/article/details/92397390