Learn how to reverse a String in Python.
There is no built-in function to reverse a String in Python.
The fastest (and easiest?) way is to use a slice that steps backwards, -1.
Example
Reverse the string "Hello World":
txt = "Hello World"[::-1]
print(txt)
Try it Yourself »
Example Explained
We have a string, "Hello World", which we want to reverse:
The String to Reverse
txt = "Hello World"[::-1]
print(txt)
Create a slice that starts at the end of the string, and moves backwards.
In this particular example, the slice statement [::-1] means start at the end of the string and end at position 0, move with the step -1, negative one, which means one step backwards.
Slice the String
txt = "Hello World"[::-1]
print(txt)
Now we have a string txt that reads "Hello World" backwards.
Print the String to demonstrate the result
Print the List
txt = "Hello World"[::-1]
print(txt)
Create a Function
If you like to have a function where you can send your strings, and return them backwards, you can create a function and insert the code from the example above.
Example
def my_function(x):
return x[::-1]
mytxt = my_function("I wonder how this text looks like backwards")
print(mytxt)
Try it Yourself »
Example Explained
Create a function that takes a String as an argument.
Create a Function
def
my_function(x):
return x[::-1]
mytxt = my_function("I wonder how this text looks like backwards")
print(mytxt)
Slice the string starting at the end of the string and move backwards.
Slice the String
def my_function(x):
return x[::-1]
mytxt = my_function("I wonder how this text looks like backwards")
print(mytxt)
Return the backward String
Return the String
def my_function(x):
return x[::-1]
mytxt = my_function("I wonder how this text looks like backwards")
print(mytxt )
Call the function, with a string as a parameter:
Call the Function
def my_function(x):
return x[::-1]mytxt = my_function("I wonder how this text looks like
backwards")print(mytxt)
Print the result:
Print the Result
def my_function(x):
return x[::-1]
mytxt = my_function("I wonder how this text looks like backwards")
print(mytxt)Python String is the collection of the Unicode character. Python has numerous functions for string manipulation, but Python string library doesn't support the in-built "reverse()" function. But there are various ways to reverse the string. We are defining the following method to reverse the Python String.
- Using for loop
- Using while loop
- Using the slice operator
- Using the reversed() function
- Using the recursion
Using for loop
Here, we will reverse the given string using for loop.
Output:
('The original string is: ', 'JavaTpoint') ('The reverse string is', 'tniopTavaJ')
Explanation-
In the above code, we have declared the reverse_string() function and passed the str argument. In the function body, we have declared empty string variable str1 that will hold the reversed string.
Next, the for loop iterated every element of the given string, join each character in the beginning and store in the str1 variable.
After the complete iteration, it returned the reverse order string str1 to the caller function. It printed the result to the screen.
Using while loop
We can also reverse a string using a while loop. Let's understand the following example.
Example -
Output:
('The original string is : ', 'JavaTpoint') ('The reversed string using a while loop is : ', 'tniopTavaJ')
Explanation:
In the above code, we have declared a str variable that holds string value. We initialized a while loop with a value of the string.
In each iteration, the value of str[count - 1] concatenated to the reverse_String and decremented the count value. A while completed its iteration and returned the reverse order string.
Using the slice ([]) operator
We can also reverse the given string using the extended slice operator. Let's understand the following example.
Example -
Output:
('The original string is : ', 'JavaTpoint') ('The reversed string(using extended slice syntax) is : ', 'tniopTavaJ')
Explanation:
Generally, a slice operator accepts three parameters - start, stop and step. We provided the no value to start and end index, which indicates the start index is 0 and the end is n-1 by default. The step size is -1; it means the string continues the traverse from the end and goes to the 1 index position.
Using reverse function with join
Python provides the reversed() function to reverse the string. Let's understand the following example.
Example -
Output:
('The original string is : ', 'JavaTpoint') ('The reversed string using reversed() is : ', 'tniopTavaJ')
Explanation:
In the function body, we declared the empty string separated by .dot operator. The reversed() string returned the reverse string it joined with the empty string separated using the join() function.
Using recursion()
The string can also be reversed using the recursion. Recursion is a process where function calls itself. Consider the following example.
Example -
Output:
('The original string is : ', 'JavaTpoint') ('The reversed string(using reversed) is : ', 'tniopTavaJ')
Explanation:
In the above code, we have defined a function that accepts the string as an argument.
In the function body, we defined the base condition of recursion, if a length of a string is 0, then the string is returned, and if not then we called the function recursively.
The slice operator slice the string expects the first character and concatenated to the end of the slice string.