by @codeexamples

How to reverse a string in Python

Home » python » How to reverse a string in Python

To reverse a string in python, we can use the slice syntax by passing the step of -1 and leaving beginning and end positions.

Here is an example.

mystring = "python"

reversed_string = mystring [::-1]

print(reversed_string)

Output:

nohtyp

Similarly, we can also use the join() function and reversed function to reverse a string.

mystring = "python"

reversed_string = ''.join(reversed(mystring))

print(reversed_string)