python 输出字符串中每个字符的个数

#输出每个字符的个数,  则需要展示字符名称 , 和值, 则需使用字典

 

def str_c(st):
dic = {}
i = 0
while i < len(st):
if st[i] in dic:
dic[st[i]] += 1
else:
dic[st[i]] = 1
i += 1
for tm in dic.items():
print(tm)


if __name__ == "__main__":
s = 'sklhsss2klj'
str_c(s)

 

posted @ 2021-03-17 11:04  ldlzzb  阅读(633)  评论(0)    收藏  举报