4 Different Python Program To Reverse A String

python program to reverse a string

In this tutorial, you will learn to write a Python program to reverse a string.

1: Python program to reverse a string using slicing

You can reverse the string using string slicing like this:

string = "hello world"
reversed_string = string[::-1] #this will reverse the string
print(reversed_string)

Output

dlrow olleh

2: Reverse a string using a loop

Create another variable to store the reversed string.

You can use a loop to iterate through the string.

Run the loop from the (length of the string – 1) to -1 with step -1.

Add each element to the variable reversed string.

You can use any loop you want.

Here is an example of the implementation of a Python program using for loop that can you can use for string reversal.

string = "hello world"
reversed_string = ""
for i in range(len(string)-1, -1, -1): #run the loop from [(length of string) - 1] to -1 with step -1
    reversed_string += string[i]
print(reversed_string)

Output

dlrow olleh

3: Python program to reverse a string using reversed() function

You can use the built-in reversed() function to reverse the string.

The reversed() function returns an iterable reversed object of what you passed as an argument.

After that, you can use the join() method.

The join() method takes all items in an iterable and joins them into one string.

string = "hello world"
reversed_string = ""
reversed_string = reversed_string.join(reversed(string)) #revesing the string
print(reversed_string)

Output

dlrow olleh

You can also iterate through the object after using the reversed() method.

string = "hello world"
reversed_string = ""
string_rev = reversed(string)
for each_character in string_rev:
    reversed_string = reversed_string + each_character
print(reversed_string)

Output

dlrow olleh

4: Python program to reverse a string using a recursive function

If you have a good grip on recursion, you can try this program.

Otherwise, you can understand recursion here and master recursion with these solved problems.

def reverse_string(string):
    if len(string) == 0:
        return string
    else:
        return reverse_string(string[1:]) + string[0]
        #string[1:0] means the entire string except first character
        #string[0] means the first character of string
        #this recursive function is taking the first element of the string and placing it at the until the string is reversed

string = "hello world"
reversed_string = reverse_string(string)
print(reversed_string)

We passed the string “hello world” to the reversed_string() function.

The length of this string is not equal to zero. So, the else part will be executed.

string[1:] means the entire string except for the first character.

In our example of “hello world” string[1:0] means “ello world”.

And string[0] represents the first character of the string.

string[0] is added at the end of string[1:] such that:

“ello world” + “h” = “ello worldh”

This is repeated with recursion until the entire string is reversed.

Output

dlrow olleh

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x