How to Reverse a String in Python | Python Tutorial

How to Reverse a String in Python.

In Python, there is no, in-built function for reverse a string in the string library. Here discuss five methods to reverse a string in Python. The ways to reverse a string are,

  1. Slicing method
  2. For loop
  3. While loop
  4. List
  5. Recursion

Sample string input and reverse output

sample reversed string.

To reverse a string, here I am using Visual Studio as a Python editor. Visit the tutorial to learn about how to run a Python script.

Method 01- Using the slicing method

Python supports string slicing. When doing slicing, it creates substrings.

Slicing syntax 👇

s[start : end : step]
  • s – Given string
  • start – Starting index
  • end – ending index
  • step – Process would take from start to end

To reverse the string easily and quickly, we use -1 as a step.

The meaning of the slice statement "[:: -1]"

Here, no fields for start and end indexes. So, it indicates default values as '0' for the start index and the length of string for the end index. Default value for set is '1' . When put '-1' as the set value, we can reverse a string. Below is the complete code for reverse a string using the slicing method.

name = "Gayani"
print(name[::-1])
code for method 1 in Python.

Slicing is a very simple method and it reduces time consumption.

Method 2 – Using for loop to reverse a string in Python

To print every character of a string, we can use a code like below.

name = "Gayani"
for char in name:
    print(char)

Here we use another variable to store the output/reverse string. It declares as a empty string(Ex: temp = ' ').

for char in name: 👉 Here, the value of the name is "Gayani". Initially, the char value will be 'G'. So control goes to the for loop body. Then it executes,

temp = char + temp

Initially, the temp variable use as the empty string and the char is 'G'. So, it concatenates 'G' and empty string. 👉 "G" + " " . It gives only "G". That "G" stores in the temp variable.

first step of for loop to reverse a String in Python.

Then again controls go to the for loop and the char value becomes 'a'. That value will come to the for loop body. Now char value is 'a' and temp value is 'G'. It will do 'a' + 'G'. So "aG" stores in the temp variable.

second step of for loop.

This process continues until the char value becomes 'i'. Finally, "inayaG" stores in the temp variable.

final step of for loop - Reverse a String in Python.

After all the iterations, the control comes from the for loop. Then it prints the value of the temp variable.

Below is the complete code for reverse a string using a for loop.

name = "Gayani"
temp = ''
for char in name:
    temp = char + temp
print(temp)
code for method 2 in Python.

Method 3 – Using the while loop

While loop keeps and executes until the condition is met. Here also we use another variable to store the output/reverse string. It is declared as an empty string (Ex: temp = ' '). We can calculate the length of the given string such as below.

length = len(name)-1

After applying the length to the while loop as below, it executes "until length>=0".

while length>=0:
    temp = temp + name[length]
    length = length - 1

Below is the complete code for reverse a string using a while loop.

name = "Gayani"
temp = ''
length = len(name)-1
while length>=0:
    temp = temp + name[length]
    length = length - 1
print(temp)
code for reverse a string using a while loop in Python.

Method 4 – Using a list to reverse a string in Python

  • Code for convert a string to the list, we use the list() function.
temp_list = list(name)
  • Code for reverse a list we use reverse() function.
temp_list.reverse()
  • To convert reversed list to a string we use the join method.
print(''.join(temp_list))

Below is the complete code for reverse a string using a list.

name = "Gayani"
temp_list = list(name)
temp_list.reverse()
print(''.join(temp_list))
code for reverse a string using a list in python.

Method 5 – Using recursion

  • Function for the reverse a string.
def reverse(str):
  • Base condition/recursion breaking condition.
if len(str) == 0:
        return str
  • Recursive condition
reverse(str[1:]) + str[0]
  • To call the function.
print(reverse("Gayani"))

Below is the complete code for reverse a string using recursion.

def reverse(str):
    if len(str) == 0:
        return str
    else:
        return reverse(str[1:]) + str[0]
print(reverse("Gayani"))
code for reverse a string using recursion in python.

The Slicing method is the best way to reverse a string in Python. No need to apply any complex methods for slicing such as loops.

Visit the previous tutorial about How to check Python Version.

Gayani Nishadika

Author of Get Basic Idea - Knowledge Base / Undergraduate - Faculty of Information Technology | University of Moratuwa.

Leave a Reply

Your email address will not be published.