Python2018-字符串中字符个数统计1 编写程序,完成以下要求:
统计字符串中,各个字符的个数
⽐如:"hello world" 字符串统计的结果为: h:1 e:1 l:3 o:2 d:1 r:1 w:1
字符串长度怎么数python1print("-"*50)
2print("*"*50)
3 currentstr = input("PLease input a sentence, the program will cal the word num:")
4
5 place('','')## Delete the 'Space' value
6 newstr=''#a new string to store the result
7for i in currentstr:
unt(i)<=1):# if  there is only one word in currentstr
9                newstr=newstr+i
10                newstr=newstr+':'
11                newstr=newstr+str(1)+''
12else:#the word has 2 or more numbers
unt(i)<1): # has not register in the new string
14                        newstr= newstr+i
15                        newstr=newstr+':'
16                        newstr= newstr+unt(i))+''
17
18print("-"*50)
19print("The word number in this sentence is : %s"%newstr)
20print("-"*50)
21
结果如下:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。