The hint tells you to end the range at 0 but in the solution it ends at -1. Can someone please explain why?
They’re just saying that the reverse will end on zero, working from right to left, rather than start on zero, working from left to right.
range(start, stop, step) start = len(word) - 1 # this is the last index stop = -1 # the included end will be zero step = -1 # negative means right to left
I would argue that is a question you can answer yourself by considering what range promises to do with the values that you give to it.
And if you don’t know what it promises then that’s the real problem, and that’s what you would solve. So where does this promise go? That’s what documentation is. For anything you use, read its documentation (how else could you possibly leverage something?)
Codecademy might not make it clear, but programmers are professional googlers and documentation readers. We leverage specifications to make things happen, so obviously we need to read those specs.
And this goes for any language, including HTML and CSS, one might add. Specs are usually dry reading; as a learner they are akin to a medical dictionary but we do grow into them and with that familiarity are able to find meaningful and useful information on demand. If one has the capacity to memorize en masse, a single reading would be enough. For the average learner it will take several readings with repeated readings over the years for refresh and updates.
Thank you for your advice, I have googled for documentation and have read up on this and now understand it. Will do this in the future before asking questions
A lot of things have some documentation attached to them, try sending them into the help function:
Thanks for explaining this! I, too, was confused at the clue. This explanation should be in the lesson. Really appreciate this answer!
So great - this is why I come to the discussions: to see if there’s a more efficient way to coding the same thing. Thank you!
Can this one work without throwing an exception?
a= [ word[-1-i] for i in range(len(word))]
Did you try it? Has the concept of comprehensions been taught, yet? If not then it is not an acceptable solution for the introduction to strings exercises.
A post was split to a new topic: A list comprehension that actually worked
Is this a good solution for solving this challenge without having to use range()?
new_word = ’ ’
for ele in word:
new_word += word[-1]
word = word[:-1] #basically we are trimming the argument word, every time the last word is added to a newly created string
As it stands each slice [:-1] is creating a new string every time it is used which is a little inefficient. You’re using a loop which goes through every character in the string (assigned to
ele). Perhaps you could make use of that instead.
Such a concise and simple method!
Just this worked for me:
I dont understand what the 3 x -1 is doing in the solution code if someone could explain?
reverse = “”
for i in range(len(word)-1,-1,-1):
reverse += word[i]
Hi ehpsauce - Thank you for sharing working solution. Could you please explain how and/or why the for loop encounters the letters at the end of the string (parameter input as it seems in this case?) first? Does the question make sense? Result is after letter I see (result = letter + result). I am not understanding how letter pulls in reverse order (or is something else occurring?).
Let’s take a look at these two lines.
for letter in word: result = letter + result
Here, we are iterating over a string,
word. Each time the loop iterates,
letter is assigned the value of the next character in
To demonstrate this, this loop would print out each letter in the word "hello".
for letter in "hello": print(letter) # prints # h # e # l # l # o
The second line of the above code concatenates
result, then assigns that string to
letter is the first string being concatenated, it comes first in the final string. This is repeated every loop iteration until all the
word have been iterated through.
To demonstrate this, this code would print "hello" backwards.
message = "" for letter in "hello": message = letter + message print(message) # prints olleh
Let’s walk through the code step by step. First, the variable
message is declared and assigned an empty string. Then, the loop begins.
The first time the loop iterates,
letter is assigned the value of
"h", since it is the first character in
"hello". We then concatenate
letter, which is
"h" in this iteration, with
message, which is currently an empty string, giving us the string
"h". The result of this concatenation,
"h", is assigned to
The second time the loop iterates,
letter is assigned the value of
"e". We then concatenate
letter, which is
"e" in this iteration, with
message, which is currently assigned the value of
"h", giving us the string
"eh". The result of this concatenation,
"eh", is assigned to
This repeats until all of the characters in
"hello" have been iterated over. Printing
message will now output the string
Welcome to the forums!
Thank you dr_victoria - The explanation (especially) helps alot!
result = “”
for letter in word:
result = letter
In the above example, is result = letter returning/concatenating y first (as opposed to h in the hello example)?
By that statement the result is always going to be one letter. Is the intention to grow a string?