w3resource
Python Exercises

Python Exercise: Reverse a string

Python Functions: Exercise-4 with Solution

Write a Python program to reverse a string.

Sample Solution:-

Python Code:

def string_reverse(str1):

    rstr1 = ''
    index = len(str1)
    while index > 0:
        rstr1 += str1[ index - 1 ]
        index = index - 1
    return rstr1
print(string_reverse('1234abcd'))

Sample Output:

dcba4321  

Pictorial presentation:

Python exercises: Reverse a string.

Flowchart:

Flowchart: Python exercises: Reverse a string.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python function to multiply all the numbers in a list.
Next: Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number as an argument.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming