PYTHON Who could help

+1 vote
I wanna write a program by python that calculates who many times each type of characters occur in a given string.
string is heeeeellllooooooworlddd for example
Nov 24, 2018 in Python by sarah
• 130 points

edited Nov 26, 2018 by Vardhan 636 views

3 answers to this question.

+1 vote

A quick solution is to use the collections module. Example:

import collections
import pprint
string = input('Enter string: ')
count = collections.Counter(string.lower())
value = pprint.pformat(count)
print(value)
answered Nov 26, 2018 by Omkar
• 69,210 points
0 votes

Another way to do it, without using any modules is by using dictionaries.

If you want to consider the lower-case and upper-case letters differently, then you can use this code:

dict = {}
string = input("Enter string: ")
for x in string:
    if x in dict:
        dict[x] += 1
    else:
        dict[x] = 1
print (dict)

 If the case doesn't really matter, then you can use this code:

dict = {}
string = input("Enter string: ")
string = string.lower()
for x in string:
    if x in dict:
        dict[x] += 1
    else:
        dict[x] = 1
print (dict)
answered Nov 26, 2018 by Omkar
• 69,210 points
0 votes

You can use the following code:

from collections import Counter
inp_string=input("Enter your string\n")
lis=list(inp_string)
length=len(inp_string)
cnt = Counter()
for i in lis:
     ls= set(i)
     for l in ls:
          cnt[l] +=1
print (cnt)
answered Dec 18, 2018 by Priyaj
• 58,090 points

Related Questions In Python

0 votes
1 answer

Python error: No handlers could be found for logger “xhtml2pdf”

The only solution is to define a ...READ MORE

answered Sep 5, 2018 in Python by Priyaj
• 58,090 points
1,944 views
0 votes
1 answer

Need help with Tkinter window formatting using Python

Tkininter comes with the columnspan argument to span the labels ...READ MORE

answered Sep 7, 2018 in Python by aryya
• 7,450 points
629 views
0 votes
1 answer

How to activate virtualenv in Python? Help!

One major issue I see is that ...READ MORE

answered Nov 5, 2018 in Python by Nymeria
• 3,560 points

edited Dec 17, 2018 by Nymeria 4,255 views
0 votes
1 answer

Search help using Python Console

The pydoc -k flag searches the documentation. pydoc ...READ MORE

answered Nov 8, 2018 in Python by SDeb
• 13,300 points
450 views
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,477 views
0 votes
1 answer

Removing surrounding whitespace

Try the strip() function:  s = s.strip() If you ...READ MORE

answered May 4, 2018 in Python by Nietzsche's daemon
• 4,260 points
575 views
0 votes
1 answer

Need help understanding python code

In this particular code, I think you ...READ MORE

answered Feb 19, 2019 in Python by Omkar
• 69,210 points
376 views
+4 votes
6 answers

Use of "continue" in python

The break statement is used to "break" ...READ MORE

answered Jul 16, 2018 in Python by Omkar
• 69,210 points
1,196 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP