How To Reverse A String In Python

Reversing a string in Python is easy. Unfortunately, Python does not make it immediately obvious how to do this with native modules, but no matter. You can use slicing to reverse a string in Python very easily.

Image via pixabay.com

Steps For How To Reverse A String In Python

To do this, simply create a string either in the terminal or in a text file.

I will use user input in a terminal to demonstrate this. I will style the output in italics.

x = input("Please enter your string to input: ")

Please enter your string to input: Hello there everyone

y = x[::-1]
print(y)

‘enoyreve ereht olleH’

You could either define your own string(s) or get user input. Below is how you could do it with user input in a text file.

x = input("Please enter your string to input: ")
y = x[::-1]
print(y)

Do you have anything to add to this article? Let’s discuss it in the comments below.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.