by Sai gowtham

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)

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course