Reverse


#1

This works for me, but I'm not sure I understand it fully.

This may be a stupid question but why does: PARAMETER[len(PARAMETER) - VARIABLE_1] work?

I understand we are starting the loop with var1 set to 1 for the first char which we derive from the length of PARAMETER but I'm still confused. help!


Reverse
#2

edit. I think I get it now, please correct me:

in the line "VARIABLE_2 += PARAMETER[len(PARAMETER)-VARIABLE_1]" we are defining a specific location of a char by looking at the length of the input text and subtracting 1 to gain access to the location of a char. so on the first iteration the location of the char is 2, since we count from 0,1,2?


#3

Nothing helps answer our questions like printing within the loop.

Given a string, x of some length,

>>> def reverse_print(x):
    r = ''
    for n in range(len(x) -1, -1, -1):
        print (x[n])

        
>>> reverse_print("Python!")
!
n
o
h
t
y
P

or

>>> def reversii(x):
    r = ''
    q = len(x)
    for n in range(q):
        print (x[q - n - 1])

        
>>> reversii("Python!")
!
n
o
h
t
y
P
>>>

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.