Python strings – Reverse a string

In this post we will be creating a short Python script that will reverse print a given string, to do this we will be slicing the string and then reading it backwards. The string will be given as input.

Source code can be seen below.

while True:
    txt = input("Enter String to reverse: ")
    reverse = txt[::-1]
    print(reverse)

Given the input “Hello World!” the script would return the following.

Enter String to reverse: Hello World!
!dlroW olleH
>>>

Leave a Reply