How to Reverse a String in Python?

In Python, various built-in libraries and methods have eased the life of programmers. However, a few operations, such as reversing a string, do not have a dedicated built-in method/function to address. The versatile Python language allows us to use other methods, such as slice operator, For loop, reversed function, etc., to reverse a string in Python.

This post will demonstrate all the possibilities to reverse a string in Python. This post contains the following content:

So let’s begin!

Method 1: Using the Slice Operator

The slice operator returns the specific part of the string according to the specified index value. The slice operator [n::m] takes two parameter values, the string’s starting and ending index of the string. The following code uses the string slicing operator to reverse a string in Python.

Code:

string_value = "Python Guide" 
print ("Input String: ", string_value)  

string_value = string_value[::-1]
print ("Reversed string: ",string_value)

In the above example code:

  • The string value “Python Guide” is initialized.
  • In the above code, the start index values will be set to default values of “0”. The end index value is set to “-1”, which indicates that string slicing will be started from the end side.

Output:

From the above output, it is verified that the input string has been reversed.

Method 2: Using for Loop

The “for” loop is used to reverse a string by iterating over it and appending it to an empty string. In the following code, the “for” loop method is used to reverse a string by adding it to an empty string.

Code:

string_value = "Python" 
empty_string = ""

for i in string_value: 
        empty_string = i + empty_string

print ("Input String: ", string_value)  
print("\nReverse string: ",empty_string)

In the above code:

  • The string value “Python” and empty string without containing a value is initialized in the program.
  • The “for” loop iterates over the input string, and the statement written inside the block will add the string value to an empty string from the reverse index.
  • The new empty string contains the old string value in reversed order.

Output:

The above output successfully reversed the string in Python.

Method 3: Using reversed() Function

In the following example, the “reversed()” function reverses the string value and adds it to an empty string using the “join()” function. The inbuilt “reversed()” function does not automatically reverse the string, it always reverses the string with the help of the “join()” function.

Code:

string_value = "itslinuxfoss" 
reverse_string = "".join(reversed(string_value))

print ("Input String: ", string_value)  
print("\nReverse string: ",reverse_string)

In the above code:

  • The “reversed()” function accepts the string value as an argument and retrieves the string in reversed order.
  • The reversed string is joined to a new empty string using the dot operator and the “join()” function.

Output:

It is verified from the above snippet that the string has been reversed.

Method 4: Using While Loop

In Python, the “while” loop is used along with the condition that shows that if the condition becomes “True”, the string value is decremented and appended to a new empty string in reversed order. In the example below, the “while” loop is used to reverse a string.

Code:

string_value = "itslinuxfoss" 
empty_String = "" 

count = len(string_value) 

while count > 0:  
    empty_String += string_value[ count - 1 ]
    count = count - 1 

print ("Input String: ", string_value)  
print("\nReverse string: ",empty_String)

In the above code:

  • The string value and an empty string are initialized in the program.
  • The “len()” function is used to get the value of the given string and store it in a “count” variable.
  •  Next, the “while” loop is initialized with the condition “count > 0”, and if the condition is satisfied(true), the decremented count value of the string will concatenate it into an empty string.
  • After concatenation, the index value will be decremented, and the process goes on until the condition becomes unsatisfied(false).

Output:

In the above output, the string value has been reversed.

Method 5: Using Recursion

The recursion function calls itself while executing the string value. The “slicing operator” technique is used inside the recursion function, which will reverse the given string whenever the function is called. In the example below, the “recursion” function is used to reverse a string in Python.

Code:

def reverse(string_value):  
    if len(string_value) == 0:
        return string_value  
    else:  
        return reverse(string_value[1:]) + string_value[0]  
   
string_value = "Learn Python" 
print ("Input String: ", string_value)  
print("\nReverse string: ",reverse(string_value))

In the above code:

  • The recursion function is defined with the prefix keyword “def” and accepts a string inside the parentheses of the function.
  • The “if-else” statement is initialized with conditions that if the string length is “zero”, the if block statement will execute. Similarly, if the string is non-zero, then the else block statement will return the reversed string recursively.
  • The string slicing method is used inside the other block, which will return the string value in reversed order to the main program.

Output:

The above output shows that the string value has been reversed.

That’s it from this tutorial!

Conclusion

To reverse a string in Python, the “string slicing”, “for” loop, “while” loop, “recursion” function, and “reversed()” functions are used. In Python, the “string slicing” method is the easiest and simplest method to reverse a string. The “for” loop iterates on the string and adds each string to a new empty string. The user-defined “recursion” function reverses the string by calling the function itself during the execution. Similarly, the “reversed()” function, along with the “join()” function, reversed a string by adding it to an empty string. This post has covered all possible Python methods to reverse a string.