Home > Software design >  How do I count the character in a string
How do I count the character in a string

Time:12-21

I want to count how many characters are there in a string but not occurences. For example:

test_list=("aabbccddee")

I want the result to be 5 because there are 5 characters:

(a,b,c,d,e) 

I tried using len function and count and also

from collections import defaultdict

CodePudding user response:

Use set in python.

len(set("aabbccddee"))
# returns 5

CodePudding user response:

Does this solve your problem?

from collections import Counter

given_string = "aabbccddee"
result = Counter(given_string)
print(len(result))

CodePudding user response:

Try this one:

your_str = 'aabbccddee'

result = len(set(your_str))
print(result)

CodePudding user response:

An alternative to set():

len(dict.fromkeys(test_list))
#5
  • Related