DEV Community

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

Posted on • Edited on

3 1

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.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (1)

Collapse
 
shijiezhou profile image
Shijie Zhou

It is hard to learn if you reply on default

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More