How can I remove the X’s from the garbled string?

Question

How can I remove the X’s from the garbled string?

Answer

We may be able to get away with just not printing the X’s, rather than going through all the trouble of deleting them and them making a new string!
Remember, to start a list slice at the end of a string and traverse to the beginning, we simply write nothing for the start and end indexes, and then make our stride index negative by the amount we want to traverse at each step.
Doing my_string[::-3] would give us every third character in my_string. The code for getting every other character is very similar!

3 Likes

In the hint, it says that strings, unlike lists, are not mutable.

Does this mean to say that, for instance, you can’t add a new letter to a string, like adding a new item to a list using square brackets?

We cannot add to or take away from a string. However, we can replace the string with a new value made from the old one and the new addition.

myString = "A quick brown fox jumps over the lazy "

Oops, left off a word.

myString = myString + "dog."
   new        old      new

or,

myString += "dog."
2 Likes

Hi, the lesson is this:

"garbled = ‘!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI’

The string in the editor is garbled in two ways:

  1. Our message is backwards.
  2. The letter we want is every other letter.

Use list slicing to extract the message and save it to a variable called message ."

The correct answer is this:

“message = [::-2]”

my answer:

“message = [-1:1:2]”

and there was no output, so I was wondering why so?

1 Like

Because in the correct answer they are going over the whole list and taking every second letter starting going backwards (due to the - Infront of the 2).

In yours you are taking the items in the list from the last item and tried to then go to the second item (the first would be 0) but the step you’ve given it is 2 forwards, not backwards. So from the last item it will try take every second item going up the list until it gets to the start of the list, obviously that can’t happen because it is going the wrong way, you need a - in front of the 2. But now because you want the whole list anyway you don’t need a start and end index. So you get back to [::-2].

1 Like

Thank you very much, I understand now! I didn’t understand the 1/-1 for the stride at first :sweat_smile:

I’ve gotta say your comments are most insiteful (a word?) and helpful to a guy trying to learn this challenging stuff. You and 1 other person - can’t remember his icon - I religiously read & always take away something meaningful.
I don’t know if you’re a professional programmer or a top-flight amateur, but you can do more in 4 lines of code than i can in 10!
Thanks & cheers,
Chuck

I have a problem with this exercise, when I started the code I was given to start off with was my answer to the previous exercise, and when I checked the soution it was supposed to start with the list called ‘garbled’ and then the hidden message. I did the code correctly but when I ran my code, it said it was wrong even though I had done it correctly. Resetting the exercise did not help either. Not sure what is going on and why the code is different to what its supposed to be
Thanks

I managed to get it to work properly after leaving it to load for a bit

you need to print your variable:

print(message)