FAQ: Code Challenge: String Methods - Count X

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

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

Learn Python 3

FAQs on the exercise Count X

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!

4 posts were split to a new topic: Should I use a loop based or method based solution?

Do we need to use the for loop? I went in a different path:

def count_char_x(word, x):
return len(word.split(x))-1

I have the same question.
I found this code also work.

def count_char_x(word, x):
  return word.count(x)

You’ll find a lot of lessons try to make use of the topics you have just been taught. Whilst there may be very effective ways to do this it may be worthwhile completing this with a lower level option like a simple loop if you’re just practicing loops.

Higher level languages are great but they can make you a little lazy when it comes to certain things; if you’re just doing this for practice/learning then using loops is not a bad idea.

2 Likes

the lower level options don’t make sense to me

This solution is the best solution in my undertanding .

def count_char_x(word,x):
return word.count(x)

[codebyte]

I don’t really understand the code you guys wrote . it would be nice if someone could explain it to me .
letters = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”

Write your unique_english_letters function here:

def unique_english_letters(word):
uniques = 0 : why ???
for letter in letters:
if letter in word:
uniques += 1 : why ???
return uniques

Uncomment these function calls to test your tip function:

print(unique_english_letters(“mississippi”))

should print 4

print(unique_english_letters(“Apple”))

should print 4

Let’s say we’re pioneering a language and don’t have the range of built-ins such as str.isalpha() or str.count(). That changes the landscape.

We could write our own isalpha() as a helper function.

def isalpha(s):
    return s in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”

and,

def addunique(c, s):
    return s + c if c not in s else s

then implement them,

def unique_english_letters(word):
    uniques = ''
    for char in word:
        if not isalpha(char): raise ValueError
        uniques = addunique(char, uniques)
    return uniques, len(uniques), len(word)

of which we only need the middle value to satisfy the lesson, but, hey?

w = 'supercalifragilisticexpialidocious'
print (unique_english_letters(w))
('supercalifgtxdo', 15, 34)

Were there a thousand letters the first element above would be the labels of the histogram, or frequency chart. The second value is the length of that string, and the final the length of the string we handed in.

w = 'quickbrownfoxjumpsoverthelazydog'
print (unique_english_letters(w))
('quickbrownfxjmpsvethlazydg', 26, 32)
        if not isalpha(char): raise ValueError

can be set to less lenient and just ignore the character.

        if not isalpha(char): continue

which will allow,

w = 'quick brown fox jumps over the lazy dog'
print (unique_english_letters(w))
('quickbrownfxjmpsvethlazydg', 26, 39)

This brings us to helper functions that are dedicated to one function. In that case we may as well roll them into the function and not clutter up the global namespace.

def unique_english_letters(word):
  def isalpha(s):
    return s in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  def addunique(c, s):
    return s + c if c not in s else s
  
  uniques = ''
  for char in word:
    if not isalpha(char): continue
    uniques = addunique(char, uniques)
  return uniques, len(uniques), len(w)
1 Like