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

     

结果如下:

 

转载于:https://www.cnblogs.com/robohou/p/8401705.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值