Write a Python program to reverse a string


The program is written in Python and reverses a given string. It first defines a string a and assigns it the value "Python Exercises". Then, it uses the print() function to display the original string. The next step is to create an empty string str to store the reversed string.

The program then uses a for loop to iterate over the characters of the string a in reverse order. It uses the range() function to generate a sequence of integers starting from the total length of the string a minus one, down to -1 (not inclusive), with a step of -1. The len() function returns the number of characters in the string a.

In each iteration, the current character of the string a is concatenated to the end of the string str using the += operator. This process continues until all the characters of the string a have been processed. Finally, the reversed string is displayed using the print() function.

Source Code

"""
a = "Python Exercises"
print(''.join(reversed(a)))
"""
a = "Python Exercises"
print("Original String :",a)
str = ""
tot = len(a)
for i in range(tot-1,-1,-1):
	str+=a[i]
print("Reverse String :",str)

Output

Original String : Python Exercises
Reverse String : sesicrexE nohtyP


Example Programs