Python String reverse with Examples

How to reverse a String in Python, for example, reverse SparkByExamples to selpmaxEyBkrapS. In this article, we will discuss several ways to reverse the given string in Python. The string is a set of characters similar to C, C++, and other programming languages.

1. Quick Examples of String Reverse

Following are quick examples of how to reverse a string.


# Consider the string
string1 = 'SparkByExamples'
print("Actual String: ",string1)

# Reverse the string using for loop.
string2 = ""
for i in string1:
        string2 = i + string2
print("Reversed String using for loop: ",string2)

# Reverse the string using slice
print("Reversed String using slice: ",string1[::-1])

# Reverse the string using reversed() function
print("Reversed String: ","".join(reversed(string1)))

# Reverse the string using comprehension
print("Reversed String: ","".join([string1[i] for i in range(len(string1)-1, -1, -1)]))

2. String Reverse using for Loop

By using the for loop to iterate the input string we can easily reverse the String in Python. We will concatenate reversed characters to another string object.

2.1 Syntax Structure


# Syntax
string2 = ""
for i in string1:
        string2 = i + string2

2.2 String Reverse with for loop Example

In this example, I have taken the string ‘sparkby examples – Python tutorials’ into the string1 variable and reversed this string, and stored the result in string2.


# Consider the string
string1 = 'PYTHON'
print("Actual String: ",string1)

# Reverse the string using for loop.
string2 = ""
for i in string1:
        string2 = i + string2
print("Reversed String: ",string2)

This example yields the below output.

We can see that the string is reversed. So the reversed string is stored in string2.

3. String Reverse using Slice Operator

We can also use slice operator that will return the reversed string. The last character index position is -1. So we will iterate the string from backward using this slice operator.

3.1 Slice Operator Syntax


# Here string1 is the input string
string1[::-1]

3.2 String Reverse using Slice Operator Example


# Consider the string
string1 = 'Python Examples'
print("Actual String: ",string1)

# Reverse the string using slice
print("Reversed String: ",string1[::-1])

# Output:
#Actual String:  Python Examples
#Reversed String:  selpmaxE nohtyP

We can see that the string is reversed.

4. String Reverse using reversed()

Python also provides a reversed() function that will reverse the string. It is an inbuilt function. We need to use join() method along with reversed() function to join the characters present in the string. Using built-in function are always suggestible to use over creating custom functions.

4.1 reversed() syntax


# Here string1 is the input string
"".join(reversed(string1))

4.2 String Reverse using reversed() Example


# Consider the string
string1 = 'sparkby examples - Python tutorials'
print("Actual String: ",string1)

# Reverse the string using reversed() function
print("Reversed String: ","".join(reversed(string1)))

# Output:
# Actual String:  sparkby examples - Python tutorials
# Reversed String:  slairotut nohtyP - selpmaxe ybkraps

5. String Reverse using Comprehension

In this scenario, we will use join() function along with comprehension. Here, we will use the for loop and specify the start range as the last character index position of the string and stop as -1. In this way, the string can be reversed. Finally, we will join all the characters using the join() function.

5.1 Syntax


# Here string1 is the input string
"".join([string1[i] for i in range(len(string1)-1, -1, -1)])

5.2 Example


# Consider the string
string1 = 'sparkby examples - Python tutorials'
print("Actual String: ",string1)

# Reverse the string using comprehension
print("Reversed String: ","".join([string1[i] for i in range(len(string1)-1, -1, -1)]))

# Output:
# Actual String:  sparkby examples - Python tutorials
# Reversed String:  slairotut nohtyP - selpmaxe ybkraps

6. Conclusion

In this article, you have learned how to reverse the string using 4 ways in Python by using for loop, slice() with join(), comprehensive e.t.c. Based on your application you can use any of the methods discussed above.

Leave a Reply