I am writing code that reverses a string e.g. GOOD —> DOOG w/o using the reverse() function. I’m almost there…
a = "hello"
for i in range(1,len(a)+1):
print a[-i],
When I complete the code with the join function, it doesn’t work as I expect. Any ideas why? Driving me nuts.
a = "hello"
for i in range(1,len(a)+1):
print ""join.a([-i],)
You can reverse a string in one line like this:
Slicing works like
[start:stop:step]. So when you use -1 as thestep, it prints the string in reverse.