Python sentence reverser -


i trying create program in python in user enters sentence , reversed sentenced printed. code have far is:

sentence = raw_input('enter sentence') length = len(sentence)  in sentence[length:0:-1]:     =     print a, 

when program run misses out last letter if word 'hello' print 'olle'. can see mistake?

you need remove 0 indices range, instead can use :

sentence[length::-1] 

also not don't need loop on string , use assignments , length can print reversed string.

so following code job :

print sentence[::-1] 

demo :

>>> s="hello" >>> print s[::-1] 'olleh' 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -