{"id":72,"date":"2017-02-18T03:30:56","date_gmt":"2017-02-18T03:30:56","guid":{"rendered":"http:\/\/www.programminginpython.com\/2023\/03\/30\/python-program-to-check-whether-a-number-is-palindrome-or-not\/"},"modified":"2025-05-13T11:35:14","modified_gmt":"2025-05-13T11:35:14","slug":"python-program-check-palindrome","status":"publish","type":"post","link":"https:\/\/www.programminginpython.com\/python-program-check-palindrome\/","title":{"rendered":"Python program to check whether a number is Palindrome or not"},"content":{"rendered":"<p>Hello folks, welcome back to <a href=\"\/\" target=\"_blank\" rel=\"noopener\">programminginpython.com<\/a>.\u00a0 Here in this post, I will share a simple Python program that checks whether a number is a palindrome or not. Let&#8217;s code a simple program to check palindrome in Python.<br \/>\nGenerally, a number is said to be a palindrome number if its reverse is the same as the original number.<\/p>\n<p>For Example, 121 is a palindrome as its reverse is also 121 whereas, 231 is not a palindrome as its reverse is 132.<\/p>\n<p><iframe title=\"Python program to check whether a number is Palindrome or not\" width=\"640\" height=\"360\" data-src=\"https:\/\/www.youtube.com\/embed\/3VQNdXA2IPI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" data-load-mode=\"1\"><\/iframe><\/p>\n<p style=\"text-align: center;\"><strong>You can watch the video on YouTube\u00a0<a href=\"https:\/\/www.youtube.com\/watch?v=3VQNdXA2IPI\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a><\/strong><\/p>\n<p class=\"extra_btn_para\"><span class=\"wdg\"> <a href=\"https:\/\/github.com\/avinashn\/programminginpython.com\/blob\/master\/palindrome.py\" target=\"_blank\" rel=\"noopener noreferrer\"> <i class=\"fa fa-github fa-lg\"><\/i> Program on GitHub<\/a><\/span><\/p>\n<div class=\"visualize_iframe\">\n<h4 class=\"post_h4\">Palindrome number \u2013 Code Visualization<\/h4>\n<p><iframe src=\"https:\/\/pythontutor.com\/iframe-embed.html#code=num%20%3D%20input%28'Enter%20any%20number%20%3A%20'%29%0Atry%3A%0A%20%20%20%20val%20%3D%20int%28num%29%0A%20%20%20%20if%20num%20%3D%3D%20str%28num%29%5B%3A%3A-1%5D%3A%0A%20%20%20%20%20%20%20%20print%28'The%20given%20number%20is%20PALINDROME'%29%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20print%28'The%20given%20number%20is%20NOT%20a%20palindrome'%29%0Aexcept%20ValueError%3A%0A%20%20%20%20print%28%22That's%20not%20a%20valid%20number,%20Try%20Again%20!%22%29&amp;codeDivHeight=400&amp;codeDivWidth=350&amp;cumulative=false&amp;curInstr=5&amp;heapPrimitives=false&amp;origin=opt-frontend.js&amp;py=3&amp;rawInputLstJSON=%5B%221245%22%5D&amp;textReferences=false\" width=\"800\" height=\"500\" frameborder=\"0\"> <\/iframe><\/p>\n<\/div>\n<h4 style=\"color: #dd9933;\">Task :<\/h4>\n<p>To check whether a number is a palindrome or not<\/p>\n<h4 style=\"color: #dd9933;\">Approach :<\/h4>\n<ul>\n<li>Read an input number\u00a0using <span title=\"in Python 3\"><code>input()<\/code><\/span> or <span title=\"in Python 2\"><code>raw_input()<\/code><\/span>.<\/li>\n<li>Check whether the value entered is an integer or not.<\/li>\n<li>We convert that integer number to a string <code>str(num)<\/code>.<\/li>\n<li>Now we use advanced slice operation <code>[start:end:step]<\/code> leaving the <code>start<\/code> and <code>end<\/code> empty and giving <code>step<\/code> a value of -1, this slice operation reverses the string.<\/li>\n<li>Now check whether the reverse is equal to num,<\/li>\n<li>If <code>reverse<\/code> is equal to <code>num<\/code>, the number is a palindrome<\/li>\n<li>When <code>reverse<\/code> is not equal to <code>num<\/code>, it is not a palindrome<\/li>\n<\/ul>\n<p class=\"extra_btn_para\"><span class=\"wdg\"> <a href=\"https:\/\/github.com\/avinashn\/programminginpython.com\/blob\/master\/palindrome.py\" target=\"_blank\" rel=\"noopener noreferrer\"> <i class=\"fa fa-github fa-lg\"><\/i> Program on Github<\/a><\/span><\/p>\n<h4 style=\"color: #dd9933;\">Program :<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">num = input('Enter any number : ')\r\ntry:\r\n    val = int(num)\r\n    if num == str(num)[::-1]:\r\n        print('The given number is PALINDROME')\r\n    else:\r\n        print('The given number is NOT a palindrome')\r\nexcept ValueError:\r\n    print(\"That's not a valid number, Try Again !\")<\/pre>\n<h4 style=\"color: #dd9933;\">Output :<\/h4>\n<figure id=\"attachment_126\" class=\"wp-caption aligncenter\" style=\"width: 535px;\" aria-describedby=\"caption-attachment-126\">\n<figure id=\"attachment_508\" aria-describedby=\"caption-attachment-508\" style=\"width: 519px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-508 lazyload\" data-src=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/palindrome.png\" alt=\"Python program to check whether a number is Palindrome\" width=\"519\" height=\"71\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 519px; --smush-placeholder-aspect-ratio: 519\/71;\" \/><figcaption id=\"caption-attachment-508\" class=\"wp-caption-text\">Python program to check whether a number is Palindrome<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_509\" aria-describedby=\"caption-attachment-509\" style=\"width: 535px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-509 lazyload\" data-src=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/palindrome1.png\" alt=\"Python program to check whether a number is Palindrome\" width=\"535\" height=\"67\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 535px; --smush-placeholder-aspect-ratio: 535\/67;\" \/><figcaption id=\"caption-attachment-509\" class=\"wp-caption-text\">Python program to check whether a number is Palindrome<\/figcaption><\/figure>\n<p><figure id=\"attachment_510\" aria-describedby=\"caption-attachment-510\" style=\"width: 534px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-510 lazyload\" data-src=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/palindrome2.png\" alt=\"Python program to check whether a number is Palindrome\" width=\"534\" height=\"73\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 534px; --smush-placeholder-aspect-ratio: 534\/73;\" \/><figcaption id=\"caption-attachment-510\" class=\"wp-caption-text\">Python program to check whether a number is Palindrome<\/figcaption><\/figure><\/figure>\n<p>&nbsp;<\/p>\n<p class=\"extra_btn_para\"><span class=\"wdg\"> <a href=\"https:\/\/github.com\/avinashn\/programminginpython.com\/blob\/master\/palindrome.py\" target=\"_blank\" rel=\"noopener noreferrer\"> <i class=\"fa fa-github fa-lg\"><\/i> Program on GitHub<\/a><\/span><\/p>\n<p>Please feel free to check other math-related\u00a0programs <a href=\"https:\/\/programminginpython.com\/category\/math-programs\/\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a> or some <a href=\"https:\/\/programminginpython.com\/category\/basic-programs\/\" target=\"_blank\" rel=\"noopener noreferrer\">basic<\/a> programs <a href=\"https:\/\/programminginpython.com\/category\/basic-programs\/\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<div class=\"course_suggestion\">\n<h5>Course Suggestion<\/h5>\n<p>Want to be strong at OOP in Python? If yes, I would suggest you take the course below.<br \/>\nCourse: <a href=\"https:\/\/www.eduonix.com\/object-oriented-programming-in-python-aided-with-diagrams\/UHJvZHVjdC04MDgwMDA=\" target=\"_blank\" rel=\"noopener noreferrer\"><br \/>\nObject Oriented Programming in Python <\/a><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>A simple\u00a0python program which checks whether a number is a palindrome or not. Generally, a number is said to be a palindrome number if its reverse is same as the original number. For Example, 121 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":210,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[31,25,24,26,29,30],"class_list":["post-72","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-basic-programs","tag-catch","tag-else","tag-if","tag-numbers","tag-palindrome","tag-try"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python program to check whether a number is Palindrome or not - Programming In Python<\/title>\n<meta name=\"description\" content=\"Python program to check whether a number is Palindrome or not. By reversing a string and comparing with original checks us for palindrome.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.programminginpython.com\/python-program-check-palindrome\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python program to check whether a number is Palindrome or not - Programming In Python\" \/>\n<meta property=\"og:description\" content=\"Python program to check whether a number is Palindrome or not. By reversing a string and comparing with original checks us for palindrome.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.programminginpython.com\/python-program-check-palindrome\/\" \/>\n<meta property=\"og:site_name\" content=\"Programming In Python\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/programminginpython\" \/>\n<meta property=\"article:published_time\" content=\"2017-02-18T03:30:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-13T11:35:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/fb_palindrome.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"AVINASH NETHALA\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@python_pip\" \/>\n<meta name=\"twitter:site\" content=\"@python_pip\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"AVINASH NETHALA\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python program to check whether a number is Palindrome or not - Programming In Python","description":"Python program to check whether a number is Palindrome or not. By reversing a string and comparing with original checks us for palindrome.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.programminginpython.com\/python-program-check-palindrome\/","og_locale":"en_US","og_type":"article","og_title":"Python program to check whether a number is Palindrome or not - Programming In Python","og_description":"Python program to check whether a number is Palindrome or not. By reversing a string and comparing with original checks us for palindrome.","og_url":"https:\/\/www.programminginpython.com\/python-program-check-palindrome\/","og_site_name":"Programming In Python","article_publisher":"https:\/\/www.facebook.com\/programminginpython","article_published_time":"2017-02-18T03:30:56+00:00","article_modified_time":"2025-05-13T11:35:14+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/fb_palindrome.png","type":"image\/png"}],"author":"AVINASH NETHALA","twitter_card":"summary_large_image","twitter_creator":"@python_pip","twitter_site":"@python_pip","twitter_misc":{"Written by":"AVINASH NETHALA","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.programminginpython.com\/python-program-check-palindrome\/#article","isPartOf":{"@id":"https:\/\/www.programminginpython.com\/python-program-check-palindrome\/"},"author":{"name":"AVINASH NETHALA","@id":"https:\/\/www.programminginpython.com\/#\/schema\/person\/9a3c14fe46d422ebf783ee61de1e788c"},"headline":"Python program to check whether a number is Palindrome or not","datePublished":"2017-02-18T03:30:56+00:00","dateModified":"2025-05-13T11:35:14+00:00","mainEntityOfPage":{"@id":"https:\/\/www.programminginpython.com\/python-program-check-palindrome\/"},"wordCount":299,"commentCount":0,"publisher":{"@id":"https:\/\/www.programminginpython.com\/#organization"},"image":{"@id":"https:\/\/www.programminginpython.com\/python-program-check-palindrome\/#primaryimage"},"thumbnailUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/palindrome_1920_4001.png","keywords":["catch","else","if","numbers","Palindrome","try"],"articleSection":["Basic Programs"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.programminginpython.com\/python-program-check-palindrome\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.programminginpython.com\/python-program-check-palindrome\/","url":"https:\/\/www.programminginpython.com\/python-program-check-palindrome\/","name":"Python program to check whether a number is Palindrome or not - Programming In Python","isPartOf":{"@id":"https:\/\/www.programminginpython.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.programminginpython.com\/python-program-check-palindrome\/#primaryimage"},"image":{"@id":"https:\/\/www.programminginpython.com\/python-program-check-palindrome\/#primaryimage"},"thumbnailUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/palindrome_1920_4001.png","datePublished":"2017-02-18T03:30:56+00:00","dateModified":"2025-05-13T11:35:14+00:00","description":"Python program to check whether a number is Palindrome or not. By reversing a string and comparing with original checks us for palindrome.","breadcrumb":{"@id":"https:\/\/www.programminginpython.com\/python-program-check-palindrome\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.programminginpython.com\/python-program-check-palindrome\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.programminginpython.com\/python-program-check-palindrome\/#primaryimage","url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/palindrome_1920_4001.png","contentUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/palindrome_1920_4001.png","width":1920,"height":400,"caption":"Python program to check whether a number is Palindrome or not"},{"@type":"BreadcrumbList","@id":"https:\/\/www.programminginpython.com\/python-program-check-palindrome\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.programminginpython.com\/"},{"@type":"ListItem","position":2,"name":"Python program to check whether a number is Palindrome or not"}]},{"@type":"WebSite","@id":"https:\/\/www.programminginpython.com\/#website","url":"https:\/\/www.programminginpython.com\/","name":"Programming In Python","description":"All About Python","publisher":{"@id":"https:\/\/www.programminginpython.com\/#organization"},"alternateName":"pip","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.programminginpython.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.programminginpython.com\/#organization","name":"Programming In Python","alternateName":"PIP","url":"https:\/\/www.programminginpython.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.programminginpython.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/04\/pip_logo_500_500.png","contentUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/04\/pip_logo_500_500.png","width":500,"height":500,"caption":"Programming In Python"},"image":{"@id":"https:\/\/www.programminginpython.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/programminginpython","https:\/\/x.com\/python_pip","https:\/\/www.youtube.com\/programminginpython","https:\/\/github.com\/avinashn\/programminginpython.com"]},{"@type":"Person","@id":"https:\/\/www.programminginpython.com\/#\/schema\/person\/9a3c14fe46d422ebf783ee61de1e788c","name":"AVINASH NETHALA","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.programminginpython.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ed52e7670d7db94820c7430d324103ccdecb16d86611d5b29064aa9ce25a958b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ed52e7670d7db94820c7430d324103ccdecb16d86611d5b29064aa9ce25a958b?s=96&d=mm&r=g","caption":"AVINASH NETHALA"},"sameAs":["https:\/\/www.programminginpython.com\/"],"url":"https:\/\/www.programminginpython.com\/author\/avinash\/"}]}},"jetpack_featured_media_url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/palindrome_1920_4001.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts\/72","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/comments?post=72"}],"version-history":[{"count":5,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts\/72\/revisions"}],"predecessor-version":[{"id":1027,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts\/72\/revisions\/1027"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/media\/210"}],"wp:attachment":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/media?parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/categories?post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/tags?post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}