To reverse a string in Python, you can use slicing with a step of -1.
In this example,
str1
with a value of 'Hello World'
.str1
with a step of -1
.str1 = 'Hello World'
reversed_str1 = str1[::-1]
print('Reversed string is:', reversed_str1)
Reversed string is: dlroW olleH
In this example,
str2
with a value of 'apple'
.str2
with a step of -1
.str2 = 'apple'
reversed_str2 = str2[::-1]
print('Reversed string is:', reversed_str2)
Reversed string is: elppa
In this tutorial, we learned How to Reverse a String in Python language with well detailed examples.