Python2018-字符串中字符个数统计

1 编写程序,完成以下要求:

  • 统计字符串中,各个字符的个数
  • 比如:"hello world" 字符串统计的结果为: h:1 e:1 l:3 o:2 d:1 r:1 w:1
  •  1 print("-"*50)
     2 print("*"*50)
     3 currentstr = input("PLease input a sentence, the program will cal the word num:")
     4 
     5 currentstr=currentstr.replace(' ','')## Delete the 'Space' value
     6 newstr=''  #a new string to store the result
     7 for i in currentstr:
     8         if(currentstr.count(i)<=1):# if  there is only one word in currentstr
     9                 newstr=newstr+i
    10                 newstr=newstr+':'
    11                 newstr=newstr+str(1)+' '
    12         else:#the word has 2 or more numbers
    13                 if(newstr.count(i)<1): # has not register in the new string
    14                         newstr= newstr+i
    15                         newstr=newstr+':'
    16                         newstr= newstr+str(currentstr.count(i))+' '
    17 
    18 print("-"*50)
    19 print("The word number in this sentence is : %s"%newstr)
    20 print("-"*50)
    21

     

结果如下:

 

posted on 2018-02-01 23:08  克拉波隆方程  阅读(5193)  评论(0编辑  收藏  举报

导航