python reverse words in string code example

Example 1: python reverse string

'String'[::-1] #-> 'gnirtS'

Example 2: how to reverse word order in python

## initializing the string
string = "I am a python programmer"
## splitting the string on space
words = string.split()
## reversing the words using reversed() function
words = list(reversed(words))
## joining the words and printing
print(" ".join(words))

Example 3: python reverse a string

#linear

def reverse(s): 
  str = "" 
  for i in s: 
    str = i + str
  return str

#splicing
'hello world'[::-1]

Example 4: how to reverse string in python

txt = "Hello World"[::-1]
print(txt)