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

 

Answer:

Untitled 19.jpg

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.

Ask a doubt
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, Social Science, Physics, Chemistry, Computer Science at Teachoo.