banner



How To Reverse A String In Python Using For Loop

Python Reverse String

Python String doesn't take a built-in contrary() function. Notwithstanding, there are various ways to reverse a string in Python.

1. How to Reverse a Cord in Python?

Some of the common ways to reverse a string are:

  • Using Slicing to create a reverse copy of the cord.
  • 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() office with reversed() iterator
  • Creating a list from the string and then calling its contrary() function
  • Using Recursion

1.1) Python Contrary String using Slicing

                                  def reverse_slicing(s):     render s[::-one]  input_str = 'ABç∂EF'  if __name__ == "__main__":     print('Opposite String using slicing =', reverse_slicing(input_str))                              

If yous run to a higher place Python script, the output will exist:

                                  Reverse String using slicing = Iron∂çBA                              

1.2) Reverse String using For Loop

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

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

1.iii) Opposite a String using While Loop

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

i.four) Opposite a String using bring together() and reversed()

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

1.5) Python Reverse String using List contrary()

                                  def reverse_list(s):     temp_list = listing(due south)     temp_list.contrary()     return ''.join(temp_list)                              

i.vi) Python Reverse String using Recursion

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

2. Best Style to Reverse a String in Python

We can opposite a string through multiple algorithms. We take already seen half dozen of them. But which of them y'all should cull to opposite a cord.

We tin apply timeit module to run multiple iterations of these functions and get the boilerplate 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 i,00,000 times using the timeit module and got the average of the best 5 runs.

                                  $ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_slicing("ABç∂EF"*ten)' 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: ii.46 usec per loop  $ python3.seven -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_join_reversed_iter("ABç∂EF"*10)' 100000 loops, best of v: ii.49 usec per loop  $ python3.seven -m timeit --number 100000 --unit of measurement usec 'import string_reverse' 'string_reverse.reverse_for_loop("ABç∂EF"*10)' 100000 loops, best of 5: 5.v usec per loop  $ python3.7 -chiliad timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_while_loop("ABç∂EF"*10)' 100000 loops, best of v: 9.four usec per loop  $ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_recursion("ABç∂EF"*x)' 100000 loops, best of v: 24.3 usec per loop                              

Python Reverse String

The below tabular array presents the results and slowness of an algorithm from the best one.

Algorithm TimeIt Execution Time (Best of 5) Slowness
Slicing 0.449 usec 1x
Listing reverse() 2.46 usec 5.48x
reversed() + join() ii.49 usec v.55x
for loop 5.5 usec 12.25x
while loop ix.4 usec 20.94x
Recursion 24.3 usec 54.12x

three. Summary

Nosotros should utilise slicing to reverse a string in Python. Its code is very simple and pocket-size and we don't need to write our ain logic to opposite the string. Likewise, it'south the fastest manner to opposite a cord as identified past the above examination executions.

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

four. References

  • reversed() API Doc
  • str.bring together() API Doc

How To Reverse A String In Python Using For Loop,

Source: https://www.journaldev.com/23647/python-reverse-string

Posted by: randallhatione.blogspot.com

0 Response to "How To Reverse A String In Python Using For Loop"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel