  Reverse string in Python (5 different ways)

File handling | Loops | Python Methods and Functions | String Variables

Using a loop

Output:

The original string is: Geeksforgeeks The reversed st ring (using loops) is: skeegrofskeeG

Explanation: In the above code, we call a function that loops the line that iterates over each element and intelligently concatenates each character at the beginning, to get a reversed line.

Using recursion

 # Python code to reverse the line # using a loop   def reverse (s): str = "" for i in s: str = i + str return str   s = "Geeksforgeeks"   print ( "The original string is:" , end = "") print (s)   print ( "The reversed string (using loops) is:" , end = "") print (reverse (s))
 # Python code to reverse the string # using recursion    def reverse (s): if len (s) = = 0 :   return s   else : return reverse (s [ 1 : ]) + s [ 0 ]   s = "Geeksforgeeks"   print ( "The original string is:" , end = "") print (s)    print ( " The reversed string (using recursion) is: " , end = " ") print (reverse (s))

Output:

The original string is: Geeksforgeeks The reversed string (using recursion) is: skeegrofskeeG

Explanation: In the above code, a string is passed as an argument to the recursive function to reverse the string. In a function, the basic condition is that if the length of the string is 0, the string is returned. If not 0, the inverse function is called reversely to cut a portion of the string other than the first character and concatenate the first character with the end of the chopped string.

Stack Usage

 # Python code to reverse the line # using the stack   # Function for creating an empty stack. This # initializes the stack size to 0 def createStack (): stack = [] return stack   # Function for determining the stack size def size (stack): return len (stack)    # The stack is empty if the size is 0 def isEmpty (stack):   if size (stack) = = 0 :   return true    # Function for adding an item to the stack. This # increases the size by 1 def push (stack, item): stack.append (item)   # Function to remove an item from the stack. # Decreases size by 1 def pop (stack) : if isEmpty (stack) : return return stack.pop ()   # Stack-based function to reverse a string def reverse (string):  n = len (string)   # Create empty stack stack = createStack ()   # Push all characters in the string onto the stack for i in range ( 0 , n, 1 ):   push (stack, string [i])   # Make the line empty, since everything # characters are stored on the stack string = ""   # Pop all line characters and put # them back to line for i in range ( 0 , n, 1  ): string + = pop (stack)   return string   # Driver code s = "Geeksforgeeks" print ( "The original string is:" , end = " ") print (s) print ( "The reversed string (using stack) is : " , end = " ") print (reverse (s))

Output:

The original string is: Geeksforgeeks The reversed string (using stack) is: skeegrofskeeG

Explanation: An empty stack was created. One by one, the characters in the string are pushed onto the stack.
One by one, all characters are popped off the stack and returned to the string.

Using the extended slice syntax

 # Python code to reverse the string # use advanced slice syntax   # Function to reverse the string def reverse (string): string = string [:: - 1 ] return string    s = "Geeksforgeeks"   print ( "The original string is:" , end = "") print (s)   print ( "The reversed string (using extended slice syntax) is:" , end = "") print (reverse ( s))

Output:

The original string is: Geeksforgeeks The reversed string (using extended slice syntax) is: skeegrofskee G

Explanation: The extended slice suggests putting the "step" field as [start, stop, step] , and the field without specifying the beginning and end indicates the default value of 0 and the length of the line, respectively, and " -1 " indicates start from end and end to start, hence reversing the line.

Using reverse

 # Python code to reverse the string # using reversed ( )   # Function to reverse the string def reverse (string): string = "". join ( rev ersed (string)) return string   s = " Geeksforgeeks "    print ( "The original string is: " , end = " ") print (s)   print ( "The reversed string ( using reversed) is: " , end = " ") print (reverse (s))

Output:

The original string is: Geeksforgeeks The reversed string (using reversed) is: skeegrofskeeG

Explanation: The reversed () function returns the reverse iterator of the given string and then its elements are concatenated into an empty string, separated with join (). And the reverse order string is generated.

This article courtesy of Manjeet Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.