0
Just see the documentation of range
. But basically, a range
has 3 values:
- initial value
- final value
- footstep
Where the initial value is included and the end is not (for example, for a range
with initial value 1, final value 5 and step 1, the values it includes would be 1, 2, 3 and 4).
And to create a range
, you can pass 1, 2 or 3 parameters. Ex:
range(7)
: with only one parameter, this becomes the final value. The initial value is set to zero and the step will be 1. Therefore, thisrange
covers 0, 1, 2, 3, 4, 5 and 6.range(2, 7)
: with two parameters, these become the initial and final value. The step is set to 1. Therefore, thisrange
covers 2, 3, 4, 5 and 6.range(1, 7, 2)
: with three parameters, these become the initial, final and step value. Therefore, thisrange
covers the values 1, 3 and 5 (the step defines that the values jump from 2 to 2, and remember that the final value - the 7 - is not included).
Only that one range
may also be in descending order, but for this the step must be negative. For example, range(5, -1, -1)
has the initial value 5, final value -1 and step -1, so it includes the values 5, 4, 3, 2, 1 and 0, in this order.
In the case of your code, the initial value is len(nome) - 1
, that is, the size of the string nome
(len(nome)
) minus 1 (therefore, 5
). That is, first you will get the character of position 5 (which is the last, since the first position is zero), after position 4, and so on.
Just for the record, it would also be possible to do the same using enumerate
(to obtain the items and their respective indices) together with reversed
to reverse the order:
nome = 'felipe'
for indice, caractere in reversed(list(enumerate(nome))):
print(caractere, indice)
Finally, be sure to watch too other ways to invert a string.
Welcome to [en.so]! You have posted an image of the code and/or error message. Although it sounds like a good idea, it’s not! One of the reasons is that if someone wants to answer the question, they can’t copy the code and change something inside. Click on the [Edit] link and put the code/error as text. See more about this in these links - Manual on how NOT to ask questions, Post Error Message as Picture
– Lucas