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:

FAQs on the exercise Count Letters

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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

Ask or answer a question about this exercise by clicking reply (reply) below!

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

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

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!

I have a question about the suggested code:

letters=“ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”
def unique_english_letters(word):
> uniques = 0
>>for letter in letters:
>>if letter in word:
>>>uniques += 1

return uniques

In this case, calling the function with the argument ‘mississippi’ returns a value of 4. I see that there are only 4 distinct letters, but what part of the code makes it so letters aren’t added to “uniques” more than once? Thanks!

Because your code is iterating over letters which is comprised of all unique characters.

In technical terms, we wouldn’t need to iterate the long string, just the word and compare it to the letters string.

uniques = []
for letter in word:
    if letter in letters and not letter in uniques:
        uniques.append(letter)
return len(uniques)
6 Likes

Ohh, I see. That makes a lot more sense now. I was thinking of some other ways to do it, too, but your explanation helps a lot. Thank you!

3 Likes

Thanks for contributing this method too, it does illustrate and explain the function in another way!

1 Like
def unique_english_letters(word):
  unique = []
  for i in word:
    if i not in unique:
      unique.append(i)
  return len(unique)

I misunderstood at first thinking it wanted only the unique letters returned, so originally I had return unique. I read it more carefully and saw it wanted a number return so I found return len(unique) to be the best option.

what am I doing wrong here?

the error message makes so sense whatsoever;
I’ve used word as a parameter like they’ve asked me to!

Be sure to unindent out of the for loop and return uniques.

I understand that I could append an empty list to get this count. But why does this not work?
Thanks for any assistance!

image

Line 7 should not be inside the loops. Set it before the outer loop.

This returns an answer of 1 for both words. When I set the return statement inline with the out loop it returns basically a letter count of both words.
Thanks!!

image

The return statement needs to unindented so it is outside of the outer loop.

I tried that several times but got an indent error.
I went ahead and re-wrote the function to return the length of a list of unique letters. That is working.
Perhaps there was a basic error in the way I was approaching the problem.
Thanks again for your input!

You might have been on the right track before my first post. I got waylaid by the letter_count variable. This function should return a list of characters.

For instance, mississippi contains misp. The function should compose a list of letters, not count them.

Did you run that function without uncommenting the function calls at the bottom?

Hi everyone,

Could anyone tell me where is my mistake in this code:

def unique_english_letters(word):
  count = 0
  for letter in word:
    match = letters.find(letter)
    if match != -1:
      letters.replace(letter," ")
      count += 1
    return count

Thanks!

str.replace() is greedy , or in other words applies globally to the string. All matching characters are replaced, not just the first one.

If I’m to understand the name of the function in literal terms it would mean there is only one of a certain letter in the word (a string).

The cheap and dirty way would be to parse out a set from the word.

return len(set(list(word)))

Of course that will never do if we are just learning to build algorithms to return the same findings.

One such approach would be to build temporary string and then add only letters that are not already in that string, to the string. We must assume there is a global, letters for this example (as with the above posted sample).

temp = ""
for letter in word:
    if letters.find(letter) > -1:
        if letter not in temp:
            temp += letter
return len(temp)

Another approach, although less of a naive algorithm, would be to use the built in count() function.

temp = ""
for letter in word:
    if letters.find(letter) > -1:
        if word.count(letter) == 1:
            temp += letter
return len(temp)

And we could go on to find other approaches, as well. The bottom line, as I perceive this problem is to find only unique occurrences of each letter and ignore any that are duplicated.

The string is immutable, .replace() method returns a copy of the string after replacement. Also, you are dealing with the word, not the letters string provided at the beginning.
Try this intead:

def unique_english_letters(word):
  count = 0
  for letter in word:
    match = word.find(letter)
    if match != -1:
      count += 1
      new_word = word.replace(letter," ")  # both "" or " "(with space) work
      word = new_word
  return count

or a simpler version:

def unique_english_letters(word):
  count = 0
  for letter in word:
    if letter in word:
      count += 1
      word = word.replace(letter,"") 
  return count

I think this approach you mentioned is better, which we don’t even need a long helper string (“letters”) at all.

It would really help if we had a link to the this exercise so the finer points can be ironed out. What are we after? Only unique letters that are not duplicated? Or only a list of letters that appear in the string.

mississippi

Only m is unique, but there are four letters in all, misp. So what’s it to be?

Good point which I didn’t go into since I scrapped that approach…

Quote from the exercise, exercise link

The function should return the total number of unique letters in the string.

:grinning:

1 Like