How to reverse words in a string with Python

2 Answers

0 votes
def reverse_words(input): 
    words = input.split(" ") 
  
    words = words[-1::-1] 

    words = ' '.join(words) 
      
    return words 
  

s = 'python java c++ c#'

print(reverse_words(s))


'''
run:

c# c++ java python

'''

 



answered Jun 29, 2019 by avibootz
0 votes
s = "python java php c#"

s =  " ".join(reversed(s.split(" "))) 

print(s)


'''
run:

c# php java python

'''

 



answered Jan 12, 2020 by avibootz
...