5 Ways To Reverse a String in Python

In Python, a string is a collection of alphabets, words, or other characters; it is one of the primitive data structures. Either single or double quotation marks surround strings in Python. To reverse a string is to make its characters (including spaces) read backwards; there is no built-in function to achieve this, but you can do it using any of these five means.

How To Reverse A String in Python

1. Reverse A String By Slicing

We use the slice() function in Python to slice a tuple in whatever way you want, and it returns an object; however, when you slice a string, a string is returned. The syntax for slicing a string in Python is as follows:

var = "Sample string"print(var[start index:end index:step])

The start and end index specify the positions to begin and end the slice; step is optional, and it specifies how many steps to take after each slice. If the start index is left empty, it’ll automatically be at position 0; if the end index is left empty, it’ll end at the last available position; if the step is a negative integer, the slice will go backward. Putting all this together to reverse a string using the slice function would be as follows:

string = "CloudDevelop"

2. Reverse A String Using The For Loop

In Python, a for loop iterates over a sequence, which could be a list, tuple, or a string. Within a for loop, you can give a command that runs until a condition is met or the sequence ends. The syntax for looping over a string in Python is:

for a in "CloudDevelop":

The output you’ll get in the shell for doing this would be one character at a time in normal order. To reverse the string and make it output as one text, we need to create a variable with an empty string outside the for loop. In the loop, we’ll append each character in the sequence to the empty variable; then, we’ll print out the variable. Here’s what that looks like:

temp = ""
string = "CloudDevelop"
for a in string:
  temp = a + temp

3. Reverse A String Using While Loop

while loop is a loop in Python that keeps running until a specified condition is met. If you want to keep printing the string “CloudDevelop” a specific number of times, you’ll assign a variable with that number and run the while loop like this:

i = 5
while i > 0:
  i -= 1

To reverse the string, you first need to make the variable a dynamic value equal to the length of the string. Then, you’d need to create a temp variable that contains an empty string and initiate a while loop that appends the characters on the string from behind, and you print out the final result.

string = "CloudDevelop"
length = len(string) -1
temp = ""
while length >= 0:
  temp = temp + string[length]
  length = length -1

4. Reverse A String Using The Reversed Function

Python’s reversed() function returns an iterator that accesses any sequence in the reverse order. However, the returned iterator will not be a string, and you’d need a loop or another method to get a string in return.

In our case, we’ll use the join() method; this method can join all items in the iterator returned by the reversed() function into one string. To use the join method, you have to indicate what you want to use as the separator in front of it; if you don’t want anything to separate each character, place an empty quote. The final syntax should look like this:

string = "CloudDevelop"

5. Reverse A String Using List – Reverse

The reverse() method can be used on lists in Python to reverse their order. The syntax is:

fruits = ['apple', 'banana', 'cherry']

However, this method only works on lists and won’t work on a string. To reverse our string using the reverse method, we would need to go over the following steps:

  • Convert the string into a list using the list() method.
  • Reverse the new list.
  • Then we’ll convert the reversed list into a string using the join() method.

Here’s what it’ll look like:

string = "CloudDevelop"
temp_list = list(string )

Leave a Comment