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 42 views

3 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
+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
• 66,880 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
• 66,880 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
• 56,140 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
• 56,140 points
115 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 ariaholic
• 7,320 points
36 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,500 points

edited Dec 17, 2018 by Nymeria 104 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
• 10,780 points
17 views
+4 votes
6 answers
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
30 views
0 votes
1 answer

Need help understanding python code

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

answered Feb 19 in Python by Omkar
• 66,880 points
35 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
• 66,880 points
40 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.