Python String doesn’t have a built-in reverse() function. However, there are various ways to reverse a string in Python.

Python Reverse String

Some of the common ways to reverse a string are:

  • Using Slicing to create a reverse copy of the string.
  • Using for loop and appending characters in reverse order
  • Using while loop to iterate string characters in reverse order and append them
  • Using string join() function with reversed() iterator
  • Creating a list from the string and then calling its reverse() function
  • Using Recursion

Let’s look at these algorithms implementations to reverse a string in Python.

Python Reverse String using Slicing

Copy
def reverse_slicing(s): return s[::-1] input_str = 'ABç∂EF' if __name__ == "__main__": print('Reverse String using slicing =', reverse_slicing(input_str))

If you run above Python script, the output will be:

Copy
Reverse String using slicing = FE∂çBA

Python Reverse String using For Loop

Copy
def reverse_for_loop(s): s1 = '' for c in s: s1 = c + s1 # appending chars in reverse order return s1 input_str = 'ABç∂EF' if __name__ == "__main__": print('Reverse String using for loop =', reverse_for_loop(input_str))

Output: Reverse String using for loop = FE∂çBA

Python Reverse String using While Loop

Copy
def reverse_while_loop(s): s1 = '' length = len(s) - 1 while length >= 0: s1 = s1 + s[length] length = length - 1 return s1 input_str = 'ABç∂EF' if __name__ == "__main__": print('Reverse String using while loop =', reverse_while_loop(input_str))

Python Reverse String using join() and reversed()

Copy
def reverse_join_reversed_iter(s): s1 = ''.join(reversed(s)) return s1

Python Reverse String using List reverse()

Copy
def reverse_list(s): temp_list = list(s) temp_list.reverse() return ''.join(temp_list)

Python Reverse String using Recursion

Copy
def reverse_recursion(s): if len(s) == 0: return s else: return reverse_recursion(s[1:]) + s[0]

Best Way to Reverse a String in Python

We can reverse a string through multiple algorithms. We have already seen six of them. But which of them you should choose to reverse a string.

We can use timeit module to run multiple iterations of these functions and get the average time required to run them.

All the above functions are stored in a python script named string_reverse.py. I executed all these functions one by one for 1,00,000 times using the timeit module and got the average of best 5 runs.

Copy
$ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_slicing("ABç∂EF"*10)' 100000 loops, best of 5: 0.449 usec per loop $ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_list("ABç∂EF"*10)' 100000 loops, best of 5: 2.46 usec per loop $ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_join_reversed_iter("ABç∂EF"*10)' 100000 loops, best of 5: 2.49 usec per loop $ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_for_loop("ABç∂EF"*10)' 100000 loops, best of 5: 5.5 usec per loop $ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_while_loop("ABç∂EF"*10)' 100000 loops, best of 5: 9.4 usec per loop $ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_recursion("ABç∂EF"*10)' 100000 loops, best of 5: 24.3 usec per loop

Python Reverse String

Below table presents the results and slowness of an algorithm from the best one.

AlgorithmTimeIt Execution Time (Best of 5)Slowness
Slicing0.449 usec1x
List reverse()2.46 usec5.48x
reversed() + join()2.49 usec5.55x
for loop5.5 usec12.25x
while loop9.4 usec20.94x
Recursion24.3 usec54.12x

Summary

We should use slicing to reverse a string in Python. Its code is very simple and small and we don’t need to write our own logic to reverse the string. Also, it’s the fastest way to reverse a string as identified by the above test executions.

You can checkout complete python script and more Python examples from our GitHub Repository.