How to reverse the order of letters in a string in python -


in python, how reverse order of words in string , reverse order of letters in word.

for example, if input is:

hello world 

the output should be:

olleh dlrow 

my attempt:

a=input('line: ')  print(a[::-1])  

your desired output conflicts description of requirements viz "how reverse order of words in string , reverse order of letters in word.". same reversing string, have provided solution. instead, reverse the letters in each word, retain order of words, can use split() , reverse slice ([::-1]) on each word.

s = "hello world" word in s.split():     print word[::-1], 

or, this:

print ' '.join(word[::-1] word in s.split()) 

the above assumes not need retain exact whitespace between words.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -