Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the number of unique characters in a string Python using only for loops and ifel operations

Tags:

python

Like title says, I am supposed to find the number of unique characters in a string. Here's the catch, no arrays or even while loops (although for loop can accomplish this). I have tried but I can't seem to do it without writing a bunch of ridiculous if-el statements under a for loop checking each char value against each other.

like image 591
Son Gohan Avatar asked Feb 21 '15 04:02

Son Gohan


People also ask

How do you count the number of characters in a string loop in Python?

Inside the Python For Loop, we used the If statement to check whether any character in a string is equal to the given character or not. If true, then count = count + 1. if(t == t) – Condition is True. if(u == l) – Condition is false.


1 Answers

Use this:

len(set("stackoverflow"))  # returns 12
like image 168
Vlad Havriuk Avatar answered Sep 17 '22 18:09

Vlad Havriuk