Reverse a String in Python

Python doesn't provide a built in way for reversing the string. This article explains different ways of reversing a string using python code.

Python Program to Reverse a String

Using List, reverse() and join()

list1 = list(original_string)

print("String converted to list:")
print(list1)

list1.reverse()
print("List Reversed: ")
print(list1)

reversed_string = "".join(list1)

print ("The original string  is : ") 

print (original_string) 

print ("The reversed string is : ") 

print(reversed_string)

Output:

String converted to list:
['C', 'o', 'm', 'p', 'u', 't', 'e', 'r', 'S', 'c', 'i', 'e', 'n', 'c', 'e', 'A', 'i']

List Reversed: 
['i', 'A', 'e', 'c', 'n', 'e', 'i', 'c', 'S', 'r', 'e', 't', 'u', 'p', 'm', 'o', 'C']
The original string  is : ComputerScienceAi
The reversed string is : iAecneicSretupmoC
Explanation: Here we are converting given string into a list of characters then we are using built in python function reverse() to reverse the list. Finally we are converting the list back to string by joining the characters using join() function to obtain reverse string.

Using Loop

# Python code to reverse a string  
# using loop

def reverse(s): 
  str = "" 
  for i in s:
    str = i + str
  return str

org_string = "computerscienceai"

print ("The original string  is :",end="") 

print (org_string) 

print ("The reversed string(using loops) is : ",end="") 

print (reverse(org_string)) 

Output:

The original string  is : ComputerScienceAi
The reversed string is : iAecneicSretupmoC

Explanation: We have written a reverse function in which we get every character of the string and attach it at the beginning in the in str variable and hence obtain the reversed string of original string.

Reverse a String Using Extended Slice Syntax

# Python code to reverse a string  
# using extended slice syntax

original_string = "computerscienceai"

# reverse a string using extended slice
reverse = original_string[::-1];

print ("The original string  is :", end="") 

print (original_string) 

print ("The reversed string is : ",end="") 

print (reverse)

Output:

The original string  is : ComputerScienceAi
The reversed string(using loops) is : iAecneicSretupmoC
Explanation: Extended Slice provides an extra field called "step". If we don't define start and stop in slice syntax [start, stop, step] it takes default value of 0 for start and string length for stop. Now the remaining part "-1" indicates starting from the end and stop at start (because value for stop is the length of the string) and hence we get reversed string for the original string.

Reverse a String using reversed() function

# Python code to reverse a string  
# using reversed() function

# Function to reverse a string 

def reverse(string): 
    string = "".join(reversed(string)) 
    return string

original_string = "ComputerScienceAi"

print ("The original string  is : ",end="")

print (original_string) 

print ("The reversed string is : ",end="") 

print (reverse(original_string))