Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

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

Posted on • Edited 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 linea==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 canfind the square root of any number with python in multiple ways without any issues.
If you are started in programming then here isroadmap to learn programming online for free.

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
shijiezhou profile image
Shijie Zhou
Living life openly. This year I set the following goals. Below you can find the progress on each.
  • Location
    Salt Lake City
  • Education
    University of California, San Diego | B.A.
  • Work
    Software Engineer at MMSG
  • Joined

It is hard to learn if you reply on default

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Coding ...
  • Joined

More fromDigvijay Singh

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp