Reverse string in Python (6 different ways)
Last Updated :
23 Sep, 2023
Python string library doesn’t support the in-built “reverse()” as done by other python containers like list, hence knowing other methods to reverse string can prove to be useful. This article discusses several ways to achieve it in Python.
Example:
Input: Geeksforgeeks
Output: skeegrofskeeG
Reverse a string in Python using a loop
In this example, we call a function to reverse a string, which iterates to every element and intelligently joins each character in the beginning so as to obtain the reversed string.
Time complexity: O(n)
Auxiliary Space: O(1)
Implementation:
Python3
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))
|
Output
The original string is : Geeksforgeeks
The reversed string(using loops) is : skeegrofskeeG
Reverse a string in Python using recursion
The string is passed as an argument to a recursive function to reverse the string. In the function, the base condition is that if the length of the string is equal to 0, the string is returned. If not equal to 0, the reverse function is recursively called to slice the part of the string except the first character and concatenate the first character to the end of the sliced string. ‘
Implementation:
Python3
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
Time complexity: O(n), for recursion to reverse
Auxiliary Space: O(n), for recursion call stack
Reverse string in Python using stack
An empty stack is created. One by one character of the string is pushed to the stack. One by one all characters from the stack are popped and put back to a string.
Time complexity: O(n)
Auxiliary Space: O(n)
Implementation:
Python3
def createStack():
stack = []
return stack
def size(stack):
return len (stack)
def isEmpty(stack):
if size(stack) = = 0 :
return true
def push(stack, item):
stack.append(item)
def pop(stack):
if isEmpty(stack):
return
return stack.pop()
def reverse(string):
n = len (string)
stack = createStack()
for i in range ( 0 , n, 1 ):
push(stack, string[i])
string = ""
for i in range ( 0 , n, 1 ):
string + = pop(stack)
return string
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
Reverse string in Python using an extended slice
Extended slice offers to put a “step” field as [start, stop, step], and giving no field as start and stop indicates default to 0 and string length respectively, and “-1” denotes starting from the end and stop at the start, hence reversing a string.
Time complexity: O(n)
Auxiliary Space: O(1)
Implementation:
Python3
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 : skeegrofskeeG
Reverse string in Python using reversed() method
The reversed() returns the reversed iterator of the given string and then its elements are joined empty string separated using join(). And reversed order string is formed.
Time complexity: O(n)
Auxiliary Space: O(n)
Implementation:
Python3
def reverse(string):
string = "".join( reversed (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
Reverse string in Python using list comprehension()
List comprehension creates the list of elements of a string in reverse order and then its elements are joined using join(). And reversed order string is formed.
Time complexity: O(n)
Auxiliary Space: O(1)
Implementation:
Python3
def reverse(string):
string = [string[i] for i in range ( len (string) - 1 , - 1 , - 1 )]
return "".join(string)
s = "Geeksforgeeks"
print ( "The original string is : " , s)
print ( "The reversed string(using reversed) is : " , reverse(s))
|
Output
The original string is : Geeksforgeeks
The reversed string(using reversed) is : skeegrofskeeG
Reverse string in Python using the function call
Function to reverse a string by converting string to list then reversed it and again convert it to string.
Time complexity: O(n)
Auxiliary Space: O(1)
Implementation:
Python3
def reverse(string):
string = list (string)
string.reverse()
return "".join(string)
s = "Geeksforgeeks"
print ( "The original string is : " , s)
print ( "The reversed string(using reversed) is : " , reverse(s))
|
Output
The original string is : Geeksforgeeks
The reversed string(using reversed) is : skeegrofskeeG
Please Login to comment...