In: Computer Science
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal
Sample output
Please enter a word: "zeus" The reverse of zeus is suez
Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
Python Code for the Reverse of a String:
#function to reverse the string
def rev(a):
#taking an empty string which will become reversed string
str = " "
# iterating through original string
for i in a:
# appending the original string in reverse order to get reversed string
str=i+str
# return the reversed string
return str
# our original string a
a="Avengers"
print("the original string is: ",end=" ")
print(a)
print("the new strings is: ",end=" ")
print (rev(a))
Screenshot of the Code :
Output: