统计字符串中,各个字符的个数(回炉练习)

Posted 赵宏涛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了统计字符串中,各个字符的个数(回炉练习)相关的知识,希望对你有一定的参考价值。

__author__ = ‘ZHHT‘
#!/usr/bin/env python
# -*- coding:utf-8 -*-

#统计字符串中,各个字符的个数
#比如:"hello world" 字符串统计的结果为: h:1 e:1 l:3 o:2 d:1 r:1 w:1

a = "hello world"
b = set(a)

for i in b:
    if i == ‘ ‘:
        c = a.count(i)
        i = ‘空格‘
        print("%s出现%d次"%(i,c))

    else:
        c = a.count(i)
        print("%s出现%-4d次"%(i,c))

  

以上是关于统计字符串中,各个字符的个数(回炉练习)的主要内容,如果未能解决你的问题,请参考以下文章