Categories
Python Answers

How to reverse a string in Python?

Spread the love

Sometimes, we want to reverse a string in Python.

In this article, we’ll look at how to reverse a string in Python.

How to reverse a string in Python?

To reverse a string in Python, we can put [::-1] after the string.

For instance, we write:

rev = 'hello world'[::-1]
print(rev)

We use -1 with no start and end indexes to reverse the string.

Therefore, rev is 'dlrow olleh'.

Conclusion

To reverse a string in Python, we can put [::-1] after the string.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *