python2018-count of characters in a string

Source: Internet
Author: User

1 Write the program to complete the following requirements:
  • The number of individual characters in the statistics string
  • For example, the result of the "Hello World" string statistic is: h:1 e:1 l:3 o:2 d:1 r:1 w:1
  • 1 Print("-"*50)2 Print("*"*50)3CURRENTSTR = input ("Please input a sentence, the program would cal the word num:")4 5Currentstr=currentstr.replace (' ',"')## Delete the ' Space ' value6Newstr="'  #a new string to store the result7  forIinchCurrentstr:8         if(Currentstr.count (i) <=1):#If there is only one word in currentstr9newstr=newstr+ITennewstr=newstr+':' OneNEWSTR=NEWSTR+STR (1) +' ' A         Else:#The word has 2 or more numbers -                 if(Newstr.count (i) <1):#Have not register in the new string -Newstr= newstr+I thenewstr=newstr+':' -Newstr= Newstr+str (Currentstr.count (i)) +' ' -  - Print("-"*50) + Print("The word number in this sentence is:%s"%newstr) - Print("-"*50) +

The results are as follows:

python2018-count of characters in a string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.