BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Program to Reverse a given String

Last Updated: June 6, 2018 by Chaitanya Singh | Filed Under: Python Examples

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:
Python Program to reverse a String using function

Related examples in Python:

    1. Python Program to calculate the length of a String
    2. Python program to check vowel and consonant
    3. Python Program to print hello World
    4. Python Program to check leap Year

Top Related Articles:

  1. Python Program to Check if a Number is Positive Negative or Zero
  2. Python Program to Find Smallest Number in a List
  3. Python program to reverse a String using Recursion
  4. Python Program to Find ASCII Value of a Character
  5. Python Program to Convert Celsius To Fahrenheit and Vice Versa

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap