{"id":3570,"date":"2020-07-13T17:58:07","date_gmt":"2020-07-13T12:28:07","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=3570"},"modified":"2021-06-14T15:14:09","modified_gmt":"2021-06-14T09:44:09","slug":"python-palindrome","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-palindrome\/","title":{"rendered":"7 Ways to Solve Palindrome Python Programs"},"content":{"rendered":"\n<p>One of the most basic and commonly asked interview questions is to check whether a string is a palindrome or not using python. <strong>A palindrome is a string or a number that, if we reverse, equals the original value<\/strong>. For example- if we reverse the string MALAYALAM, we will get back the original string. Also, if we reverse the number <strong>12321, we will get 12321 back<\/strong>. These are known as palindromes.<\/p>\n\n\n\n<p>In this article, we will learn how to check whether a string or a number is a palindrome or not in many different ways. In addition to it, we will solve some <a href=\"http:\/\/www.pythonpool.com\/pyjokes\/\" target=\"_blank\" rel=\"noreferrer noopener\">fun<\/a> questions which are commonly asked in competitions and interviews.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-palindrome\/#Checking_Whether_a_String_is_a_Palindrome_in_Python\" >Checking Whether a String is a Palindrome in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-palindrome\/#1_Check_Palindrome_Using_Slicing_in_Python\" >1. Check Palindrome Using Slicing in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-palindrome\/#2_Check_Palindrome_Using_Loop_In_Python\" >2. Check Palindrome Using Loop In Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-palindrome\/#3_Check_Palindrome_Using_reversed_Function_In_Python\" >3. Check Palindrome Using reversed() Function In Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-palindrome\/#4_Check_Palindrome_Using_Using_While_Loop_In_Python\" >4. Check Palindrome Using Using While Loop In Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-palindrome\/#5_Checking_Whether_a_Number_is_a_Palindrome_in_Python_Using_Loop\" >5. Checking Whether a Number is a Palindrome in Python Using Loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-palindrome\/#6_Checking_Whether_a_Phrase_is_a_Palindrome_in_Python\" >6. Checking Whether a Phrase is a Palindrome in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-palindrome\/#7_To_find_the_Palindromic_longest_substring_in_a_string\" >7. To find the Palindromic longest substring in a string<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-palindrome\/#Must_Read\" >Must Read<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-palindrome\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-checking-whether-a-string-is-a-palindrome-in-python\"><span class=\"ez-toc-section\" id=\"Checking_Whether_a_String_is_a_Palindrome_in_Python\"><\/span>Checking Whether a String is a Palindrome in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"http:\/\/www.pythonpool.com\/python-palindrome\/?preview_id=3570&amp;preview_nonce=2694fb5d55&amp;preview=true&amp;_thumbnail_id=3575#Checking_Whether_a_String_is_a_Palindrome_in_Python\"><\/a><a href=\"http:\/\/www.pythonpool.com\/python-palindrome\/?preview_id=3570&amp;preview_nonce=2694fb5d55&amp;preview=true&amp;_thumbnail_id=3575#Check_Palindrome_Using_Slicing_in_Python\"><\/a>Check Palindrome Using Slicing in Python<\/li><li><a href=\"http:\/\/www.pythonpool.com\/python-palindrome\/?preview_id=3570&amp;preview_nonce=2694fb5d55&amp;preview=true&amp;_thumbnail_id=3575#Check_Palindrome_Using_Slicing_in_Python\"><\/a>Check Palindrome Using reversed() Function In Python<\/li><li><a href=\"http:\/\/www.pythonpool.com\/python-palindrome\/?preview_id=3570&amp;preview_nonce=2694fb5d55&amp;preview=true&amp;_thumbnail_id=3575#Check_Palindrome_Using_Slicing_in_Python\"><\/a>Check Palindrome Using Using While Loop In Python<\/li><li><a href=\"http:\/\/www.pythonpool.com\/python-palindrome\/?preview_id=3570&amp;preview_nonce=2694fb5d55&amp;preview=true&amp;_thumbnail_id=3575#Check_Palindrome_Using_Slicing_in_Python\"><\/a>Checking Whether a Number is a Palindrome in Python Using Loop<\/li><li><a href=\"http:\/\/www.pythonpool.com\/python-palindrome\/?preview_id=3570&amp;preview_nonce=2694fb5d55&amp;preview=true&amp;_thumbnail_id=3575#Check_Palindrome_Using_Slicing_in_Python\"><\/a>Checking Whether a Phrase is a Palindrome in Python<\/li><li><a href=\"http:\/\/www.pythonpool.com\/python-palindrome\/?preview_id=3570&amp;preview_nonce=2694fb5d55&amp;preview=true&amp;_thumbnail_id=3575#Check_Palindrome_Using_Slicing_in_Python\"><\/a>To find the Palindromic longest substring in a string<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-1-check-palindrome-using-slicing-in-python\"><span class=\"ez-toc-section\" id=\"1_Check_Palindrome_Using_Slicing_in_Python\"><\/span>1. Check Palindrome Using Slicing in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We can use the concept of slicing to reverse the string, and then we can check whether the reverses string is equal to the original string or not.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef check_palindrome(string):\n   # transversing the string from last to first\n   reversed_string=string&#x5B;::-1]\n      if string==reversed_string:\n         print(string, &quot;is a palindrome&quot;)\n      else:\n         print(string, &quot;is not a Palindrome&quot;)\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nif name=='main':\n   check_palindrome(&quot;RADAR&quot;)\n   check_palindrome(&quot;PythonPool&quot;)\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\nRADAR is a palindrome\nPythonPool is not a Palindrome<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"375\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-12-1024x375.png\" alt=\"palindrome python\" class=\"wp-image-3572\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-12-1024x375.png 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-12-300x110.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-12-768x281.png 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-12-1536x562.png 1536w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-12-20x7.png 20w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-12.png 1584w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The above method is straightforward to use and also a  good method, and you can use it in competitions, but people generally don&#8217;t prefer to use it in interviews. This method is so simple, and people prefer to code from scratch to make this kind of program to show their skills. We will study that approach too in the following section.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-2-check-palindrome-using-loop-in-python\"><span class=\"ez-toc-section\" id=\"2_Check_Palindrome_Using_Loop_In_Python\"><\/span>2. Check Palindrome Using Loop In Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef is_palindrome(string):\n   reversed_string=&quot;&quot;\n   # transversing through string from last\n   for i in range(len(string),0,-1):\n      # Addind last characters of string into a new string\n      reversed_string+=string&#x5B;i-1]\n      if string==reversed_string:\n         print(&quot;Palindrome&quot;)\n      else:\n         print(&quot;Not a palindrome&quot;)\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nif __name__==__'main'__:\n   is_palindrome(&quot;racecar&quot;)\n   is_palindrome(&quot;Python&quot;)\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\nPalindrome\nNot a palindrome<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-3-check-palindrome-using-reversed-function-in-python\"><span class=\"ez-toc-section\" id=\"3_Check_Palindrome_Using_reversed_Function_In_Python\"><\/span>3. Check Palindrome Using reversed() Function In Python <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstring=&quot;MALAYALAM&quot;\n# joining characters of reversed string one by one\nreversed_string = ''.join(reversed(string))\n   if reversed_string==string:\n      print(string,&quot; is Palindrome&quot;)\n   else:\n      print(string,&quot;Not a Palindrome&quot;)\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\nMALAYALAM is Palindrome<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-4-check-palindrome-using-using-while-loop-in-python\"><span class=\"ez-toc-section\" id=\"4_Check_Palindrome_Using_Using_While_Loop_In_Python\"><\/span>4. Check Palindrome Using Using While Loop In Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&lt;pre class=&quot;wp-block-syntaxhighlighter-code&quot;&gt;def check_palindrome(string):\n   l= len(string)\n   first = 0\n   last = l-1\n   isPalindrome=True\n   # ensuring that we do not &lt;a href=&quot;http:\/\/www.pythonpool.com\/python-iterate-through-list\/&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot;&gt;iterate&lt;\/a&gt; through more than half                          #   of the list\n   while(first&lt;last):\n      # if the first character is same as last character keep     #       moving further\n      if(string&#x5B;first]==string&#x5B;last]):\n         first=first+1\n         last=last-1\n     # if the characters at first and last do not match break #      the loop\n     else:\n        isPalindrome=False\n        break\n   return isPalindrome\n\nstring=&quot;MADAM&quot;\nisPalindrome=check_palindrome(string)\nif(isPalindrome):\n   print(&quot;It is a palindrome &quot;)\nelse:\n   print(&quot;Not a Palindrome&quot;)&lt;\/pre&gt;\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\nIt is a palindrome<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-5-checking-whether-a-number-is-a-palindrome-in-python-using-loop\"><span class=\"ez-toc-section\" id=\"5_Checking_Whether_a_Number_is_a_Palindrome_in_Python_Using_Loop\"><\/span>5. Checking Whether a Number is a Palindrome in Python Using Loop<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We are going to use the following concept-<br><strong>Number=12321<br>The remainder of this number, when divided by 10, is:<br>Number%10=12321%10=1<br>Reverse_Number=Remainder=1<br>Then, we will divide the number by 10.<br>Number=Number\/10=12321\/\/10=1232<br>Remainder=1232%10=2<br>Reverse_Number=Remainder=12<br>Number= 1232\/10=123<br>Remainder=123%10=3<br>Reverse_Number=Remainder=123<br>Number= 123\/10=12<br>Remainder=12%10=2<br>Reverse_Number=Remainder=1232<br>Number= 12\/10=1<br>Remainder=1%10=1<br>Reverse_Number=Remainder=12321<\/strong><\/p>\n\n\n\n<p>Program-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumber=12321\nreverse_number=0\nn=number\n# while we have not reached the end of the number\nwhile(n !=0):\n   # finding the last element of number 'n'\n   rem = n % 10\n   reverse_number = reverse_number * 10 + rem\n   n=int(n \/ 10)\nif(number == reverse_number):\n   print(&quot;Palindrome&quot;)\nelse:\n   print(&quot;Not a Palindrome&quot;)\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\nPalindrome<\/pre>\n\n\n\n<p><br>We can also first convert the number into a string and then apply any of the methods above to check if the number is palindrome or not.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumber=12321\n# converting the number to string and then reversing it\nif str(number)&#x5B;::-1]==str(number):\n   print(&quot;Number:&quot;,number,&quot; is a Palindrome&quot;)\nelse:\n   print(&quot;Number:&quot;,number,&quot; is not a Palindrome&quot;)\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\nNumber: 12321 is a Palindrome<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-6-checking-whether-a-phrase-is-a-palindrome-in-python\"><span class=\"ez-toc-section\" id=\"6_Checking_Whether_a_Phrase_is_a_Palindrome_in_Python\"><\/span>6. Checking Whether a Phrase is a Palindrome in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Checking if a phrase is a palindrome or not is different from checking if a word is a palindrome or not. So, we cannot apply any of the above methods for a phrase.<br>For example- The phrase \u2018Too hot to hoot\u2019 is a palindrome if you ignore the upper case &#8211; lower case and spaces in the characters.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef is_palindrome(string):\n   reversed_string=&quot;&quot;\n   # Removing all the spaces\n   s=string.replace(&quot; &quot;,&quot;&quot;)\n   # making the whole string in lowercase characters \n   s=s.lower()\n   for i in range(len(s),0,-1):\n      if s&#x5B;i-1] &gt;='a' and s&#x5B;i-1]&lt;='z':\n         reversed_string+=s&#x5B;i-1]\n   if s==reversed_string:\n      print(&quot;Palindrome&quot;)\n   else:\n      print(&quot;Not a palindrome&quot;)\nif __name__=='__main__':\n   is_palindrome(&quot;Too hot to hoot&quot;)\n   is_palindrome(&quot;Python&quot;)\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\nPalindrome\nNot a palindrome<\/pre>\n\n\n\n<p>There are some other types of palindromes, like- &#8216;Is it crazy how saying sentences backward creates backward sentences saying how crazy it is.&#8217;<strong> It is different from other palindromes we have discussed until now because here, if we reverse the characters, it is not a palindrome. But if we reverse it word by word, it is a palindrome.<\/strong><\/p>\n\n\n\n<p>Program-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstring='Is it crazy how saying sentences backwards creates backwards sentences saying how crazy it is'\nstring1=string.lower()\nstring1=string.replace(&quot; &quot;,&quot;&quot;)\nnew_string=&quot;&quot;\n# Wherever there is any space make a list element\nlist1=string1.split(&quot; &quot;)\n# join the list into string starting from the last\nreverse=&quot;&quot;.join(list1&#x5B;::-1])\nreverse_string=&quot;&quot;\nfor i in reverse:\n# adding only characters in the new string\n   if i&gt;='a' and i&lt;='z': \n      reverse_string+=i \nfor i in string1:\n   if i&gt;='a' and i&lt;='z':\n      new_string+=i\nif new_string==reverse_string:\n   print(string,&quot;:Palindrome&quot;)\nelse:\n   print(string,&quot;:Not a Palindrome&quot;)\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\nIs it crazy how saying sentences backward creates backward sentences saying how crazy it is: Palindrome<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-7-to-find-the-palindromic-longest-substring-in-a-string\"><span class=\"ez-toc-section\" id=\"7_To_find_the_Palindromic_longest_substring_in_a_string\"><\/span>7. To find the Palindromic longest substring in a string<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A very common and interesting question on palindromes is to find the longest <a aria-label=\"undefined (opens in a new tab)\" href=\"http:\/\/www.pythonpool.com\/python-substring\/\" target=\"_blank\" rel=\"noreferrer noopener\">substring<\/a>, which is a palindrome from a string that may or may not be a palindrome. I suggest you try it yourself once and then look at the solution below.<br>There are many ways to solve this problem. We will go for the easiest one which all of us will be able to understand easily.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nm=&quot;&quot;\ns='babad'\nfor i in range(len(s)):\n# iterating through the string from the last\n   for j in range(len(s),i,-1):\n# ensuring that we do not iterate through more than half of \n# the string\n      if len(m)&gt;=j-i:\n         break\n      elif s&#x5B;i:j]==s&#x5B;i:j]&#x5B;::-1]:\n         m=s&#x5B;i:j]\nprint(m)\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\nbab<\/pre>\n\n\n\n<p><br>The time complexity for the above program is<strong> O(n^2)<\/strong> as we have a for loop inside a for a <a aria-label=\"undefined (opens in a new tab)\" href=\"https:\/\/en.wikipedia.org\/wiki\/For_loop\" target=\"_blank\" rel=\"noreferrer noopener\">loop<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-must-read\"><span class=\"ez-toc-section\" id=\"Must_Read\"><\/span><span class=\"has-inline-color has-luminous-vivid-orange-color\">Must Read<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"http:\/\/www.pythonpool.com\/python-lowercase\/\">How to Convert String to Lowercase in<\/a><\/li><li><a href=\"http:\/\/www.pythonpool.com\/square-root-in-python\/\">How to Calculate Square Root<\/a><\/li><li><a href=\"http:\/\/www.pythonpool.com\/python-user-input\/\">User Input | Input () Function | Keyboard Input<\/a><\/li><li><a href=\"http:\/\/www.pythonpool.com\/python-book\/\">Best Book to Learn Python<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We have studied what a palindrome is, how to check whether a string or a number is a palindrome. We have also covered some common interview questions like checking for a phrase if it is a palindrome and finding the longest substring, which is a palindrome in python. I hope you will try each problem, and please comment down if you face any issues in it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the most basic and commonly asked interview questions is to check whether a string is a palindrome or not using python. A palindrome &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"7 Ways to Solve Palindrome Python Programs\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/python-palindrome\/#more-3570\" aria-label=\"More on 7 Ways to Solve Palindrome Python Programs\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":3575,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4,15],"tags":[1494,1476,1478,1487,1492,1491,1489,1479,1480,1493,1486,1475,1488,1483,1484,1477,1481,1485,1490,1482],"class_list":["post-3570","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programs","category-tutorials","tag-build-a-palindrome-hackerrank-solution-python","tag-check-if-string-is-palindrome-python","tag-check-palindrome-python","tag-how-to-check-if-a-string-is-a-palindrome-in-python","tag-is-palindrome-python","tag-is-python-a-palindrome","tag-making-string-palindrome-python","tag-palindrome-in-python","tag-palindrome-linked-list-python","tag-palindrome-number-python","tag-palindrome-permutation-python","tag-palindrome-program-in-python","tag-palindrome-python","tag-palindrome-python-code","tag-python-how-to-check-palindrome","tag-python-palindrome","tag-python-palindrome-code","tag-python-palindrome-function","tag-python-palindrome-program","tag-python-program-for-palindrome","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>7 Ways to Solve Palindrome Python Programs - Python Pool<\/title>\n<meta name=\"description\" content=\"Strings and number that are same even if reversed are palindromes. We can check whether a string or number is Palindrome in python.\" \/>\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.pythonpool.com\/python-palindrome\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"7 Ways to Solve Palindrome Python Programs\" \/>\n<meta property=\"og:description\" content=\"One of the most basic and commonly asked interview questions is to check whether a string is a palindrome or not using python. A palindrome is a string or\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-palindrome\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-13T12:28:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-06-14T09:44:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-14.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1001\" \/>\n\t<meta property=\"og:image:height\" content=\"606\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ashwini Mandani\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ashwini Mandani\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/python-palindrome\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-palindrome\/\"},\"author\":{\"name\":\"Ashwini Mandani\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056\"},\"headline\":\"7 Ways to Solve Palindrome Python Programs\",\"datePublished\":\"2020-07-13T12:28:07+00:00\",\"dateModified\":\"2021-06-14T09:44:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-palindrome\/\"},\"wordCount\":698,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-palindrome\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-14.jpg\",\"keywords\":[\"build a palindrome hackerrank solution python\",\"check if string is palindrome python\",\"check palindrome python\",\"how to check if a string is a palindrome in python\",\"is palindrome python\",\"is python a palindrome\",\"making string palindrome python\",\"palindrome in python\",\"palindrome linked list python\",\"palindrome number python\",\"palindrome permutation python\",\"palindrome program in python\",\"palindrome python\",\"palindrome python code\",\"python how to check palindrome\",\"python palindrome\",\"python palindrome code\",\"python palindrome function\",\"python palindrome program\",\"python program for palindrome\"],\"articleSection\":[\"Programs\",\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/python-palindrome\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pythonpool.com\/python-palindrome\/\",\"url\":\"https:\/\/www.pythonpool.com\/python-palindrome\/\",\"name\":\"7 Ways to Solve Palindrome Python Programs - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-palindrome\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-palindrome\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-14.jpg\",\"datePublished\":\"2020-07-13T12:28:07+00:00\",\"dateModified\":\"2021-06-14T09:44:09+00:00\",\"description\":\"Strings and number that are same even if reversed are palindromes. We can check whether a string or number is Palindrome in python.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-palindrome\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/python-palindrome\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/python-palindrome\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-14.jpg\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-14.jpg\",\"width\":1001,\"height\":606,\"caption\":\"palindrome python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/python-palindrome\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"7 Ways to Solve Palindrome Python Programs\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056\",\"name\":\"Ashwini Mandani\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"caption\":\"Ashwini Mandani\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"7 Ways to Solve Palindrome Python Programs - Python Pool","description":"Strings and number that are same even if reversed are palindromes. We can check whether a string or number is Palindrome in python.","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.pythonpool.com\/python-palindrome\/","og_locale":"en_US","og_type":"article","og_title":"7 Ways to Solve Palindrome Python Programs","og_description":"One of the most basic and commonly asked interview questions is to check whether a string is a palindrome or not using python. A palindrome is a string or","og_url":"https:\/\/www.pythonpool.com\/python-palindrome\/","og_site_name":"Python Pool","article_published_time":"2020-07-13T12:28:07+00:00","article_modified_time":"2021-06-14T09:44:09+00:00","og_image":[{"width":1001,"height":606,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-14.jpg","type":"image\/jpeg"}],"author":"Ashwini Mandani","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Ashwini Mandani","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-palindrome\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-palindrome\/"},"author":{"name":"Ashwini Mandani","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056"},"headline":"7 Ways to Solve Palindrome Python Programs","datePublished":"2020-07-13T12:28:07+00:00","dateModified":"2021-06-14T09:44:09+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-palindrome\/"},"wordCount":698,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-palindrome\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-14.jpg","keywords":["build a palindrome hackerrank solution python","check if string is palindrome python","check palindrome python","how to check if a string is a palindrome in python","is palindrome python","is python a palindrome","making string palindrome python","palindrome in python","palindrome linked list python","palindrome number python","palindrome permutation python","palindrome program in python","palindrome python","palindrome python code","python how to check palindrome","python palindrome","python palindrome code","python palindrome function","python palindrome program","python program for palindrome"],"articleSection":["Programs","Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-palindrome\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/python-palindrome\/","url":"https:\/\/www.pythonpool.com\/python-palindrome\/","name":"7 Ways to Solve Palindrome Python Programs - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-palindrome\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-palindrome\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-14.jpg","datePublished":"2020-07-13T12:28:07+00:00","dateModified":"2021-06-14T09:44:09+00:00","description":"Strings and number that are same even if reversed are palindromes. We can check whether a string or number is Palindrome in python.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-palindrome\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-palindrome\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-palindrome\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-14.jpg","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-14.jpg","width":1001,"height":606,"caption":"palindrome python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-palindrome\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"7 Ways to Solve Palindrome Python Programs"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056","name":"Ashwini Mandani","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","caption":"Ashwini Mandani"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=3570"}],"version-history":[{"count":10,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3570\/revisions"}],"predecessor-version":[{"id":13560,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3570\/revisions\/13560"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/3575"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=3570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=3570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=3570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}