Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Reverse a String

Python Reverse a String

Document Object Reference Manual Python3 Examples

In Python, a string can be reversed in several ways. The most common is a slice operation, which is simple and efficient. Below we show how to use slicing to reverse a string.

Example

def

reverse_string

(

s

)

:

   

return

s

[

::-

1

]

Example usage

original_string

=

"Hello, World!"

reversed_string

=

reverse_string

(

original_string

)

print

(

reversed_string

)

Code analysis:

Output result:

!dlroW ,olleH

Document Object Reference Manual Python3 Examples