Exercise 25 

Write a program that asks the user to enter a word and return the opposite. Example if the user enters the word python, the program returns nohtyp.

Solution 

First method

# Ask to type a string s
s = input("Type a string s : ")
# getting the reverse of the string s
s1 = s[::-1]

print("The reverse of the string : '",s,"' is : ", s1)

Second method

# Ask to type a string s
s = input("Type a string s : ")
# initialize the reverse of the string s to empty string
rev = ""

# Building the string s in a reversed way
for x in s:
rev = x + rev
print("The reverse of the string : '",s,"' is : ", rev)

Younes Derfoufi
One thought on “Solution Exercise 25: reverse string”

Leave a Reply