0

I need to take a string input and count the occurrence of a character only once per character(should not repeat for the same character) and print it in a list or set or tuple. But I'm not getting the desired result.

Code:

string1 = input('Enter your string: ')


for letter in string1:

    string2 = string1.count(letter,0)

    ss = str(string2)

    print(ss)

Output:

Enter your string: ggddg
3
3
2
2
3
Mahesh Hegde
  • 33
  • 3
  • 12

6 Answers6

3

Depending on how you want your output to look, you can use collections.Counter, which has a dict-like interface:

from collections import Counter

user_input = input('Enter your string: ')
counts = Counter(user_input)

print(counts)  # prints: Counter({'g': 3, 'd': 2})
the_constant
  • 681
  • 4
  • 11
1

You are printing the count for each different character as many times as they appear in the string. First is g, there are 3 gs so you print 3, then another g comes and you count and print again. I think you want to print that there are 3 'g' and 2 'd'.

Try this:

letters = {}
for c in input("Enter your string: "):
    if c not in letters:
        letters[c] = 1
    else:
        letters[c] += 1

print(letters)

Output:

Enter your string: ggddg
{'g': 3, 'd': 2}
krisz
  • 2,686
  • 2
  • 11
  • 18
0

Try this:

string1 = input('Enter your string: ')
print('\n'.join(list(set([i+' '+str(string1.count(i)) for i in string1]))))

Output:

Enter your string: aaabbcca
c 2
a 4
b 2

Or try this:

string1 = input('Enter your string: ')
print('\n'.join(list(set(map(lambda x: x+' '+str(string1.count(x)),string1)))))

Output:

Enter your string: aaabbcca
c 2
a 4
b 2
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0

you could use a dict to storage that info

d = {} 

for c in string1:
    try:
        d[c] += 1
    except KeyError:
        d[c] = 1
print(d)
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
juancarlos
  • 593
  • 3
  • 9
0

I think this is what you want, try it with the sample user string

#!python3

# sample user string: fdshjkfdskhjlfdsjklfdsevcxzm,.czxm,.

user_string = input('Enter your string: ')

all_letters = []
unique_letters = []

for letter in user_string:
    all_letters.append(letter)

for unique in all_letters:
    if unique in unique_letters:
        pass
    else:
        unique_letters.append(unique)

for item in unique_letters:
    print(item + ' = ' + str(all_letters.count(item)))

# results
'''
f = 4
d = 4
s = 4
h = 2
j = 3
k = 3
l = 2
e = 1
v = 1
c = 2
x = 2
z = 2
m = 2
, = 2
. = 2
'''
Michael Swartz
  • 858
  • 2
  • 15
  • 27
0

@Mahesh, counting occurrences of a character in a string can be efficiently done by using set and dictionary as follows.

Try it online at http://rextester.com/KQV63040.

Note: I have used json module to pretty print the dictionary.

You can use OrderedDict of collections module to retain the order of keys in dictionary based on insertion.

import json

string1 = input('Enter your string: ').strip()

unique_letters = set(string1)
letters_occurrences = {}

for letter in unique_letters:
    letters_occurrences[letter] = string1.count(letter)

print() # newline

# Pretty printing the dictionary
print(json.dumps(letters_occurrences, indent=4))

» Output

Enter your string: abbacddabbc

{
    "b": 4,
    "a": 3,
    "d": 2,
    "c": 2
}
Community
  • 1
  • 1
hygull
  • 8,464
  • 2
  • 43
  • 52