Just finished Learn Python the Hard Way and I'm now working on the GitHub 100 Projects for Python.
Let me start by saying that I do understand this is the solution to the reverse a string project:
string = raw_input("> ")
print "< %r" % string[::-1]
Example: 'Hello string' >>> 'gnirts olleH'
The above program reverses the string from back to front, but what I would like is a reversal in place, so I made this (Which does what I want it to do):
myString = raw_input("> ")
running = True
while running:
myList = myString.split(' ')
myList.reverse()
myNewString = ' '.join(myList)
print myNewString
running = False
Example: 'Hello string' >>> 'olleH gnirts"
I am a brutal perfectionist and I am curious if someone knows of an even better way to acheive the same using a For-loop rather than a while-loop? Am I overlooking something obvious? Thank you!
EDIT: I do understand that the while loop here does nothing, thanks for feedback!
Here is one with a for loop
s = 'Hello World'
rev = []
for i in s.split(' '):
rev.append(i[::-1])
print ' '.join(rev)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With