FAQ: Code Challenge: String Methods - Every Other Letter

Community%20FAQs%20on%20Codecademy%20Exercises

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:

FAQs on the exercise Every Other Letter

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!

Isn’t it easier to write:

14 Likes

Would anyone be able to tell me why the given solution to this problem works? In an earlier lesson we learned that strings are immutable. It seems like the process of iterating through the letters in the given word and then adding every other one to the string every_other would necessarily require us to change the string every_other.

Is it that strings can be added to, but the individual characters in strings can’t be removed or swapped without creating a new string?

It would seem that way, although we are not changing it, but replacing it with a new composed string value

Only when we do it on the right side of an assignment (or in an argument) using concatenation (or other method) to build the desired string expression. Each time we assign it to the left side. If that is the same variable, then we have replaced its value with the new one.

if a and b are string values,

a = a + b

replaces a with the joint string value. The right hand side is not mutating a string, but creating a single string value.

a += b

says the same thing using the compact assignment operator.

Thank you MTF! Your explanation that we’re “replacing” rather than “changing” the string really clicked for me.

2 Likes

I must be forgetting something but how does python know to use “word” when iterating through “i” through the range() defined? What if we had a second variable in the function and wanted to use the range defined to iterate through that second variable and not “word”? How do you define what variable you want to the range() to refer to if there is more than one?

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

1 Like

range() just returns a sequence of integers, assigned each time to an iterator variable. It is up to you to define how to use the variable. It can be used as an index, or simply as an int

str1 = "abc"
str2 = "123"
out_str = ""
count = 0
for i in range(len(str1)):
    out_str += str1[i] * int(str2[i])    # i is an index to two different strings
    count += i        # i is an integer used in counting
print(out_str, count)

Output;

abbccc 3

ahh. Pretty simple when you put it that way :-). Thanks

1 Like

You can try this :

def every_other_letter(word):

other_letter =
for i in range(0,len(word),2):
other_letter.append(word[i])

return ‘’.join(other_letter)

I wrote this and it works perfectly in jupyter, but in codecademy i get : ‘string index out of range’

def every_other_letter(word):
    new_string = ''
    for i in range(len(word)):
        if 2*i <= len(word):
            new_string += word[2*i]
    return new_string
1 Like

image

your code works for words of uneven length, not even length

What calls did you attempt in jupyter that do work?

1 Like

it seems like i put only uneven string randomly, haha, but thanks,
have changed for this, works good now:
def every_other_letter(word):
new_string = ‘’
for i in range(len(word)):
if 2i+1 <= len(word):
new_string += word[2
i]
return new_string

I was little stuck originally, because I declared a variable to hold my new string like this:

new_string = ()

I got the rest of the code right, then couldn’t figure out why it wasn’t working and then got further confused by the answers above. They seem to my amateur eyes to be overcomplicating it somewhat.
It’s only after I fixed my original code to declare the empty string properly it worked perfectly:

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

this will result in a tuple:

new_string = ()
print(type(new_string))

Yes exactly. That was the error I was getting. Fixed by changing to

new_string = “”