In this Python tutorial, you will learn how to reverse a string.
Table Of Contents
Let’s discuss some approaches to reverse a string.
Reverse a string using the slice
Here, we will use the slicing to reverse a string. To slice a string we can use the [] operator i.e.
str[start, end, stepsize]
It will selects a substring from string. This portion of string is selected using the value of start to end using the stepsize, It returns the selected string as a slice. Now to get all the characters in string in reverse order, keep the start and end values as default. But use the stepsize as -1 i.e. str[::-1]. It will itearte the entire string from end to start in reverse order and returns a reversed string.
Frequently Asked:
Syntax:
input_str[::-1]
Where input_str is the input string to be reversed.
Example 1:
In this example, we will reverse the string – “Welcome to thisPointer”.
Latest Python - Video Tutorial
input_str="Welcome to thisPointer" print("Actual String: ",input_str) # Reverse the string input_str = input_str[::-1] print("Reversed String: ",input_str)
Output:
Actual String: Welcome to thisPointer Reversed String: retnioPsiht ot emocleW
The string is reversed.
Example 2:
In this example, we will reverse the string – “123 rthu”.
input_str="123 rthu" print("Actual String: ",input_str) # Reverse the string input_str = input_str[::-1] print("Reversed String: ",input_str)
Output:
Actual String: 123 rthu Reversed String: uhtr 321
The string is reversed.
Reverse a string using reversed()
The reversed() is a built-in function used to reverse the an iterable object. If we pass a string to the reversed() function, it will return an iterable sequence of characters of string but in reverse order. Use join() to join all the characters in the reversed sequence.
Syntax:
"".join(reversed(input_str))
where input_str is the input string to be reversed. It will return a copy of the string with reversed content.
Example 1:
In this example, we will reverse the string – “Welcome to thisPointer”.
input_str="Welcome to thisPointer" print("Actual String: ",input_str) # Reverse the string input_str = "".join(reversed(input_str)) print("Reversed String: ",input_str)
Output:
Actual String: Welcome to thisPointer Reversed String: retnioPsiht ot emocleW
The string is reversed.
Example 2:
In this example, we will reverse the string – “123 rthu”.
input_str="123 rthu" print("Actual String: ",input_str) # Reverse the string input_str = "".join(reversed(input_str)) print("Reversed String: ",input_str)
Output:
Actual String: 123 rthu Reversed String: uhtr 321
The string is reversed.
Reverse a string using reduce()
The reduce() is a built-in function used to reduce the elements of a sequence based on provided logic. We can pass a string and a lambda function i it. It will iterate over all the characters of provided string. For each character in that string, it will call the given lambda function. Inside thelambda function we will join the characters in reverse order. We can use the lambda expression – “lambda i, j: j + i” to reverse the string.
Syntax:
reduce(lambda i, j: j + i, input_str)
Where input_str is the input string to be reversed. It returns a copy of the string input_str after reversing the contents.
Example 1:
In this example, we will reverse the string – “Welcome to thisPointer”.
from functools import reduce input_str="Welcome to thisPointer" print("Actual String: ",input_str) # Reverse the string input_str = reduce(lambda i, j: j + i, input_str) print("Reversed String: ",input_str)
Output:
Actual String: Welcome to thisPointer Reversed String: retnioPsiht ot emocleW
The string is reversed.
Example 2:
In this example, we will reverse the string – “123 rthu”.
from functools import reduce input_str="123 rthu" print("Actual String: ",input_str) # Reverse the string input_str = reduce(lambda i, j: j + i, input_str) print("Reversed String: ",input_str)
Output:
Actual String: 123 rthu Reversed String: uhtr 321
The string is reversed.
Reverse a string using reverse()
Here, we will convert the string to a list of characters and then reverse the list using the reverse() method. After that, we can use the join() function to merge characters of the list from end to start. In this way, we can reverse the string.
Example 1:
In this example, we will reverse the string – “WelcometothisPointer”.
input_str="WelcometothisPointer" print("Actual String: ",input_str) # Convert string into list list1=list(input_str) # Reverse the list list1.reverse() # Reverse the string input_str = "".join(list1) print("Reversed String: ",input_str)
Output:
Actual String: WelcometothisPointer Reversed String: retnioPsihtotemocleW
The string is reversed.
Example 2:
In this example, we will reverse the string – “123rthu”.
input_str="123rthu" print("Actual String: ",input_str) # Convert string into list list1=list(input_str) # Reverse the list list1.reverse() # Reverse the string input_str = "".join(list1) print("Reversed String: ",input_str)
Output:
Actual String: 123rthu Reversed String: uhtr321
The string is reversed.
Summary
In this Python string tutorial, we have seen different ways to reverse a string. The methods we used are: slice(),reduce(),reversed() and reverse(). We can not apply reverse directly on the string, we have to convert the string into a list and apply reverse(). Finally, we have used the join() to merged reversed characters as a string from the list. Happy Learning.
Latest Video Tutorials