FAQ: Code Challenge: String Methods - Every Other Letter

This community-built FAQ covers the “Every Other Letter” 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 Every Other Letter

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!

2 posts were split to a new topic: Here’s my solution

3 posts were split to a new topic: Aren’t strings immutable?

3 posts were split to a new topic: How does python know to use our word with range()?

3 posts were split to a new topic: Why does my code work outside but not on codecademy?

3 posts were split to a new topic: Did I make a tuple?

Im just appending every other indexed letter using the modulus operator. Why wouldnt this work, don’t spaces get counted as characters?

Here is the code i wrote:

def every_other_letter(word):
New_word = “”
for i in word:
if word.index(i)%2 == 0:
New_word += i
return New_word

spaces get counted. I suppose it depends on the requirements how you would need to handle spaces.

spaces are still characters.

I can’t understand why my code is not working:

def every_other_letter(word):
    new_word = []
    word_split = word.split()
    for i in range(len(word), 2):
        new_word.append(word_split[i])
    return "".join(new_word)

It returns “IndexError: list index out of range” on line. I tried to correct the range but I was not able to. Is it possible to keep the same logic (even though I’m aware this is not the best way to solve the exercise) just correcting the range?

Something to note, split() does not split a word, but a sentence. To split a word we would use, list(). That brings up the question, why even bother since a string is iterable, already?

Could we not simply iterate the string and build a new string from the letters we extract?

s = ""
...
s += word[i]

Now the range() issue. The step is a positional argument, meaning it has to be in the third position.

range(0, len(word), 2)
2 Likes

Hi,
My code is working even without using range. Below is my program-

def every_other_letter(word):
    return word[::2]

# Uncomment these function calls to test your function:
print(every_other_letter("Codecademy"))
# should print Cdcdm
print(every_other_letter("Hello world!"))
# should print Hlowrd
print(every_other_letter(""))
# should print 

My query is : Is it necessary to use range and for loop in this program ?

Thanks

4 Likes

At this point the more practice and application the more we learn about special cases and alternate usage. Shortcuts are fine, but not if they are not backed up by clear understanding of range and loops.

2 Likes

I will keep that in my mind. Thanks.

1 Like

What is wrong with my code, I think it is correct. Displays the correct results, yet codecademy gives me a red cross? I am missing something, do help. Here is my code.

def every_other_letter(word):
  new_word = " "
  for i in range(0, len(word), 2):
    new_word = new_word + word[i]
  return new_word


# Uncomment these function calls to test your function:
print(every_other_letter("Codecademy"))
# should print Cdcdm
print(every_other_letter("Hello world!"))
# should print Hlowrd
print(every_other_letter(""))
# should print 

your new word has a leading space, of which codecademy does not approve

2 Likes

Thank you for being super prompt. Moving forward. Cheers!

I don’t understand the goal of this exercise. Even with the examples given, I really don’t get it. Can someone reformulate the question of this challenge?
Thank you so much

This exercise is asking you to return a string containing every other letter of a given input.

For example, take this function call.

every_other_letter("challenge") # should return calne

We start with the first letter, c. Then we skip a letter to get to the next one a. We repeat this until we reach the end of the string. Now we want to return a string all of the letters that were not skipped (c, a, l, n, e).

Welcome to the forums!

1 Like

When I try this, my results for

print(every_other_letter(“Hello world!”))
–> Hlloworld

I don’t understand why it would print all but the second letter and the space using modulo here. It shouldn’t in my understanding.

For the first one it works just fine

print(every_other_letter(“Codecademy”))
–> Cdcdm

.index() finds the first match, so that is a problem if you the same letter multiple times.

1 Like