4Geeks logo
4Geeks logo
About us

Learning library

For all the self-taught geeks out there, here our content library with most of the learning materials we have produces throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Full-Stack Software Developer

Data Science and Machine Learning - 16 wks

Search from all Lessons

Social & live learning

The most efficient way to learn: Join a cohort with classmates just like you, live streams, impromptu coding sessions, live tutorials with real experts, and stay motivated.

← Back to How to's

Continue learning for free about:

Edit on Github

How to reverse string in python

Written by:

Javier Leyva Seiglie

How to reverse a string in Python

The fastest and simplest way to achieve the desired result would be to use an extended slice:

1name = "Joe" 2name = name[::-1] 3print(name) # eoJ

Taking in consideration that Python library doesn´t support the built-in method reverse() as we would do, to name just one, the list container, we are left to ask: How to reverse a string in Python? Well, here are a few ways to do it, so let´s dig in:

  • Extended Slice (string[::-1])
  • Using reversed() method
  • Using loops to reverse the string
  • Using lists

Extended Slice (string[::-1])

Using steps to reverse a string is fast and simple:

1def reverseSteps(str): 2 string = str[::-1] 3 return string 4 5myString = "How to reverse a string in Python" 6 7print(reverseSteps(myString)) 8#output: nohtyP ni gnirts a esrever ot woH

Taking advantage of the extended slice fields [start:stop:step], we can reverse a string while we DO NOT stablish either start and stop fields. If we do not pass those values, default for start field will be 0 and default for the end field will be the length of the string. Passing -1 to the step field indicates that we want to start taking our steps from the end of the element and finish in the start.

Using reversed() method:

If you use the reversed() method by itself, it will throw the reference on the memory instead of the reversed string, like this:

1def reverseWithReversed(str): 2 string = reversed(str) 3 return string 4 5myString = "How to reverse a string in Python" 6 7print(reverseWithReversed(myString)) 8#output: <reversed object at 0x000001BB6DA81CF0> 9

You may ask why? Because it returns the reversed iterator of the string. We use join() to, as the name suggest, join them and achieve the desired result

1def reverseWithReversed(str): 2 string = "".join(reversed(str)) 3 return string 4 5myString = "How to reverse a string in Python" 6 7print(reverseWithReversed(myString)) 8#output: nohtyP ni gnirts a esrever ot woH

Using loops to reverse the string

Yes, we can use our old and trusty companion to reverse a string. You must be already familiarize with loops, so here is the code example to make it happen:

1def reverseLoop(str): 2 string = "" 3 for i in str: 4 string = i + string 5 return string 6 7myString = "How to reverse a string in Python" 8 9print(reverseLoop(myString)) 10#output: nohtyP ni gnirts a esrever ot woH

We iterate through the given string (myString) and store its characters on the string variable. On every loop, we will replace the string value with the new character taken from myString store it and add to the end of the string variable the value that was on the string variable on the previous loop.

Using lists

If you are a Die-Hard fan of lists, want to use the reverse() method and don´t like the other options, here´s how to do it:

1def reverseWithList(str): 2 string = list(str) 3 string.reverse() 4 return "".join(string) 5 6myString = "How to reverse a string in Python" 7 8print(reverseWithList(myString)) 9#output: nohtyP ni gnirts a esrever ot woH

We turned the string into a list, then we are able to use the reverse() method on the list we just created and then we turn it back joining the list element into a string.

Conclusion:

We covered different approaches regarding how to reverse a string in Python since the reverse() method is not supported on Strings. We discussed the steps approach, reversed() method, loops and using lists with the reverse() method and how each and every one works behind the scene. Choosing which one to use depends on how much comfortable you feel with each of the options and the needs of the project.

Hope you enjoy the reading and keep on the Geek side!