In this tutorial we will see how to reverse a String in Python using a user defined function.
Program to Reverse a String in Python
Here we have defined a function and in the function we are reversing a string using for loop. The steps are as follows:
Step 1: Create a loop to read each character of the String one by one.
Step 2: Declare an empty String.
Step 3: Concatenate the read character at the beginning of the string.
# Program published on https://beginnersbook.com # Python program to reverse a given String # Using a user-defined function def reverse(str): s = "" for ch in str: s = ch + s return s # given string mystr = "BeginnersBook" print("Given String: ", mystr) # reversed string print("Reversed String: ", reverse(mystr))
Source code of the program in PyCharm IDE: