Write a program that reads a string and check whether it is a palindrome string or not.

 

Answer:

Write a program that reads a string and check whether it is - Class 11 - Long Answer Type Questions (5 Marks each)

Code:

str=input( "Enter a string: " )

rev=str[:: -1 ]

if str==rev:

    print("Given string is a palindrome")

else:

    print("Given string is not a palindrome")  

 

Explanation:

A string is a palindrome if it reads the same forwards and backwards.

A string can be reversed in python using the slice operation . If we ignore both the indexes and give step size as -1, the string is printed in reverse.

Now the entered string and the reversed string are compared to check whether the string is a palindrome or not.

Remove Ads
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.