FAQ: Code Challenge: String Methods - Every Other Letter

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

My answer was quite same as yours, but i used .find()
The problem here is .index and .find can only point out the first occurrence.

“Hello” contains 2 “ll” which makes our answer went wrong.

Hello Community!

Why does this code give every other letter for the first word but not for the second word? It will also give me even numbers when I feed in a string of numbers. Why am I getting Hlloworld for the second call?

Thanks!

# Write your every_other_letter function here:

def every_other_letter(word):

  odd_letters = []

  for i in word:

    if word.index(i) % 2 == 0:

      odd_letters.append(i)

      odd_letters_joined = ''.join(odd_letters)

  return odd_letters_joined

# Uncomment these function calls to test your function:

print(every_other_letter("Codecademy"))

# should print Cdcdm

print(every_other_letter("0123456789"))

print(every_other_letter("Hello world!"))

# should print Hlowrd

print(every_other_letter(""))

print(every_other_letter("0123456789"))

# should print

Output:
Cdcdm
02468
Hlloworld
Traceback (most recent call last):
File “script.py”, line 16, in
print(every_other_letter(""))
File “script.py”, line 8, in every_other_letter
return odd_letters_joined
UnboundLocalError: local variable ‘odd_letters_joined’ referenced before assignment

there are several problems, first of using .index() isn’t a good idea. .index() will give you the first match. So if a letter of number occurs multiple times, that is a problem

use the loop to determine if the index is even

also, if word is an empty string, the loop doesn’t make any iteration, so odd_letters_joined isn’t named/defined, which is why you get an UnboundLocalError

Thanks stetim94,

I pulled the solution, and I see that my solution isn’t the one provided as a solution. This is one of my first attempts where anything at all worked where I didn’t use the supports provided. Thanks for your explanation of the unbound local error. That was helpful.

I thought I was using a loop to determine if the index was even. Is that what you are telling me, that using a loop to determine if the index is even is a bad idea? I see that it isn’t working, but I don’t understand why it isn’t. What’s going on?

Here’s another example:

print(every_other_letter(“eoeoeoeoeoeoeoeo”) outputs a string like this oooooooooo.

Thanks for your consideration.

you are not, you are using the .index() method. You should check the documentation on this method.

no, using the loop to determine an index is even is a very good idea. But that is not what you are currently doing