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.