Computer Science - Class 12
Chapter 14 - Functions in python

Write a Python program to reverse a string.

 

Answer:

def string_reverse (str1):

    rev_str= ""

    for i in str1:

        rev_str=i+rev_str

    return rev_str

 

str1=input( "Enter a string to reverse: " )

print("Reversed string is ",string_reverse(str1))

 

Question 6-Write a Python program to reverse a string - Teachoo.JPG

Go Ad-free

Transcript

Question 6Write a Python program to reverse a string. Defining a function to reverse a string For loop which iterates through each element of str1 Returning the reversed string Accepts a string as user input and assigns it to str1 output An empty string ‘rev_str’ is declared str1 is reversed by taking each element of str1 and assigning it to rev_str in reverse order

Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo