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

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

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

结果如下:

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

上一篇:5个提升Google Chrome浏览器运行速度的技巧


下一篇:改变Chrome浏览器主程序_缓存_个人信息路径