DEV Community

Cover image for Check Palindrome in Python Program Easy Way
Digvijay Singh
Digvijay Singh

Posted on • Updated on

Check Palindrome in Python Program Easy Way

Python is one of the easiest languages which gained a lot of popularity. The main reason why it is preferred is its simplicity and tons of library. Writing programs in other programs to do certain things is lengthy but in python, its a matter of few lines of code (maybe one line in many cases). To check palindrome in python there is just one line of logic here.

a = input()
print("It is palindrome" if a==a[::-1] else "It is NOT Palindrome")

See, one line of input and one line of logical output. It is also called the pythonic way of writing code.
To make it simple, I will break it down in parts. The above code was a compressed form of if-else block in python combined with print.
It may also be written as:

a = input()
if(a==a[::-1]):
    print("It is palindrome.")
else:
    print("It is not palindrome")

In this code the line a==a[::-1]checks if the the string is equal to its reverse string (a[::-1] reverse the string).
A simple code in a simple way.
Python has many internal libraries that make the problem solving very easy like you can find the square root of any number with python in multiple ways without any issues.
If you are started in programming then here is roadmap to learn programming online for free.

Top comments (1)

Collapse
 
shijiezhou profile image
Shijie Zhou

It is hard to learn if you reply on default