beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Python Program to Reverse a given String

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

Leave a Reply Cancel reply

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

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap