FAQ: Code Challenge: String Methods - Count Letters

This community-built FAQ covers the “Count Letters” exercise from the lesson “Code Challenge: String Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Join the Discussion. Help a fellow learner on their journey.

Agree with a comment or answer? Like () to up-vote the contribution!

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

4 posts were split to a new topic: How does the code prevent duplicates?

A post was merged into an existing topic: How does the code prevent duplicates?

3 posts were split to a new topic: What’s wrong with my code?

6 posts were split to a new topic: I don’t understand why my code returns 1?

6 posts were split to a new topic: Could I use replace to work through the letters?

2 posts were merged into an existing topic: How does the code prevent duplicates?

A post was merged into an existing topic: How does the code prevent duplicates?

What do you mean by ‘’‘unique characters’’’ ??

The goal is to capture the number of unique letters from the latin alaphabet that appear in a single word. I think examples are easier-

``````'aaaaaaaaaaaaaaaaa'  # just a single unique letter, a; the count is 1
'aaaabbbbbbbbbaabb'  # two unique letters, a & b; the count is 2
``````
1 Like
``````len(set("stackoverflow")) #returns 12
``````

Am i only one, or isn’t it easier to just write:

``````def unique_english_letters(word):
unique_letters = []

for ch in word:
if ch not in unique_letters:
unique_letters.append(ch)

return len(unique_letters)
``````

and not to use list of all unique letters if we need just unique letters in the word (“The function should return the total number of unique letters in the string.”)?

What would happen for the following string? `"Simon's"`. Is that count what you expect? Does it match the original requirements? You could argue about what a ‘word’ should be but be aware of how your function behaves and make sure its limiations are known.

2 Likes

What do you mean? `"Simon's"` will be printed as `7`. Do you mean unique english letters should be printed and not the unique characters because I also didn’t used the string given at very top to use letter from A-Z and a-z.

My interpretation of the instructions was to count the letters and letters only, hence the inclusion of the `letters` string. For the given examples it’s not much of an issue but it’s worth being aware of how that function would operate if it was fed strings that contained non-letter characters.

‘Unique letters in a string’ isn’t necessarily the same as ‘Unique characters in a string’. The function name and docstring should definitely reflect that if you ever wound up using something similar in a project.

ohhh…i see. btw due to difference between unique character and unique letter as you’ve told, I’ve changed from code…

``````#from this
def unique_english_letters(word):
k=list()
for x in word:
if x not in k:
k.append(x)
return len(k)
``````
``````#to this
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
def unique_english_letters(word):
k=list()
for x in word:
if x in letters:
if x not in k:
k.append(x)
return len(k)
``````
5 Likes

Below is the code I came up with to solve the problem. I did look at the solution because clearly I over complicated this, which I seem to do with every problem. I still dont understand how ‘in’ works when iterating through a string. In the solution. how is: ‘m’ == ‘mississippi’ true?

I had to look up a method to remove duplicates and Im happy with what I wrote but I guess am looking to get better at my approach to problems.

My solution:

``````def unique_english_letters(word):
counter = 0
bean = ''
for letter in word:
if letter not in bean:
counter += 1
bean += letter
return counter
``````

I successfully completed the exercise with the following code, but just for fun, I’m wondering if there is an opportunity to use list comprehension here? I don’t think list comprehension will work here, but I just thought I’d double check. (Note that I only used `letter in letters` because the challenge directions said to use the `letters` list in the code.) Thanks

``````#successful code without list comprehension
def unique_english_letters(word):
unique_list = []
for letter in word:
if (letter in letters) and (letter not in unique_list):
unique_list.append(letter)
return (len(unique_list))
``````

My unsuccessful attempt at list comprehension is as follows. I know I totally butchered it.

``````#unsuccessful attempt with list comprehension
#[EXPRESSION for ITEM in LIST <if CONDITIONAL>]
def unique_english_letters_lc(word):
unique_list = [letter in word if (letter in letters) and (letter not in unique_list)]
return (len(unique_list))
``````

List comprehension would be fine but you’ve missed a little piece of syntax. You need the `for elem in iterable` bit too. Be a little cautious with complex list comprehensions as some may find them harder to read.

Sometimes breaking them onto multiple lines can make them a little more readable especially if they’re a little longer, e.g.

``````lst = [
elem
for elem in iter
if elem < x
]
``````