python-reverse-string

Python Reverse String

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

Table of Contents[hide]

  • 1 1. How to Reverse a String in Python?
    • 1.1 1.1) Python Reverse String using Slicing
    • 1.2 1.2) Reverse String using For Loop
    • 1.3 1.3) Reverse a String using While Loop
    • 1.4 1.4) Reverse a String using join() and reversed()
    • 1.5 1.5) Python Reverse String using List reverse()
    • 1.6 1.6) Python Reverse String using Recursion
  • 2 2. Best Way to Reverse a String in Python
  • 3 3. Summary

1. How to Reverse a String in Python?

A portion of the normal approaches to turn around a string are:

1-plan designs essentials
  • Using Slicing to make a converse duplicate of the string.
  • Using for loop and adding characters backward request
  • Using while loop to repeat string characters backward request and add them
  • Using string join() function with reversed() iterator
  • Making a list from the string and afterward calling its reverse() function
  • Utilizing Recursion

1.1) Python Reverse String using Slicing


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:


Reverse String using slicing = FE∂çBA

1.2) Reverse String using For Loop


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

1.3) Reverse a String using While Loop


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))

1.4) Reverse a String using join() and reversed()


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

1.5) Python Reverse String using List reverse()


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

1.6) Python Reverse String using Recursion


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

2. Best Way to Reverse a String in Python

We can turn around a string through numerous calculations. We have just observed six of them. However, which of them you ought to decide to switch a string.

We can use timeit module to run numerous emphasess of these capacities and get the normal time needed to run them.

All the above capacities are put away in a python content named string_reverse.py. I executed every one of these capacities individually for multiple times utilizing the timeit module and got the normal of the best 5 runs.


$ 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

The 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

3. Summary

We should utilize cutting to invert a string in Python. Its code is basic and little and we don’t have to compose our own rationale to turn around the string. Additionally, it’s the quickest method to invert a string as distinguished by the above test executions.