reverse words in a string python code example

Example 1: how to reverse a string in python

# in order to make a string reversed in python 
# you have to use the slicing as following

string = "racecar"
print(string[::-1])

Example 2: python reverse string

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

Example 3: 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 4: how to reverse a string in python

'your sting'[::-1]

Example 5: python reverse a string

#linear

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

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

Example 6: Python reverse a string

# Library
def solution(str):
    return ''.join(reversed(str)) 
  
# DIY with recursion
def solution(str): 
    if len(str) == 0: 
        return str
    else: 
        return solution(str[1:]) + str[0]