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

Remove Ads
Teachoo · Class 12 Explore Class 12

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 is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.