Write a Python program that accepts a word from the user and reverse it

Example 1:- 

word = input("Input a word to reverse: ")

for char in range(len(word) - 1, -1, -1):
  print(word[char], end="")

output:-
Input a word to reverse: word
drow
>>> 
Example 2:-
# Python code to reverse a string
# using loop

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

word=input("enter the string: ")

print ("The original string is : ",end="")
print (word)

print ("The reversed string(using loops) is : ",end="")
print (reverse(word))
output:-
enter the string: hello
The original string is : hello
The reversed string(using loops) is : olleh
Example 3:-

Python Program to Reverse Words in a Given String in Python


def rev_words(string):  
    words = string.split(' ') 
    rev = ' '.join(reversed(words)) 
    return rev
 
s=input("enter the string: ")
print ("reverse: ",rev_words(s))

output:-
enter the string: hello world
reverse:  world hello
>>> 
Example 4:-

Reverse a String in Python

txt = "Hello World"[::-1]
print(txt)
output:-
dlroW olleH
Tag:-
How to reverse a string in Python?

Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post