string = input("Enter a string: ")
for i in range(len(string)-1, -1, -1):
print(string[i], end="")
I am confused in this start, stop, step
not sure why it is -1
in the stop bit - I tried putting 0
.
range(start, stop, step)
start: It is optional. default value is 0.
stop: It is required although the number defined in stop is not included [exclusive]
step: It is optional. default value is 1
for i in range(len(string)-1, -1, -1):
This will iterate from last character to first character
for i in range(len(string)-1, 0, -1):
This will iterate from last character to second character
For better understanding:
Ex- range(1,5) -> 1,2,3,4 #5 is not included.
Ex- range(-9,-5) -> -9,-8,-7,-6 #-5 is not included.
Ex- range(8,3,-1) -> 8,7,6,5,4 #3 is not included.