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

Given a string, reverse it and output it in reverse order.

Example 1: use string slicing

str

=

'

Runoob

'

print

(

str

[

::-

1

]

)

The output of the code above is:


boonuR

Example 2: use reversed()

str

=

'

Runoob

'

print

(

'

'

.

join

(

reversed

(

str

)

)

)

The output of the code above is:


boonuR

Document Object Reference Manual Python3 Examples