{"id":66298,"date":"2020-09-14T12:31:26","date_gmt":"2020-09-14T12:31:26","guid":{"rendered":"https:\/\/www.fita.in\/?p=66298"},"modified":"2023-10-09T12:55:10","modified_gmt":"2023-10-09T12:55:10","slug":"palindrome-program-in-python","status":"publish","type":"post","link":"https:\/\/www.fita.in\/palindrome-program-in-python\/","title":{"rendered":"Palindrome Program In Python"},"content":{"rendered":"
\r\n
In this blog we’ll go step by step to design programs to check if a string or a number is palindrome or not .You can also create your own program to solve this problem based on the algorithms.<\/p>\r\n
Note that we will be using loops, functions join and slicing methods to solve this specific problem. You can gain great insights of these topics and more about python with real-world based problems in Python Training in Chennai <\/a>or Python Training in Bangalore<\/a> at FITA.<\/p>\r\n On 02,02, 2020, it read the same in both the MM\/DD\/YYYY and the DD\/MM\/YYYY format, and at just after 2 a.m., it was 02:02:20. The next such date will come after 101 years on 12\/12\/2121.<\/p>\r\n Such patterns are palindrome. The palindrome is defined as the words, numbers or phrases that form mirror images on reversal.<\/p>\r\n A number is palindrome if the digits of it when reversed, turns out to be the original number.For eg: 12321 or 123321 have the same mirror images individually.<\/p>\r\n The string or the word aibohphobia is an example of a palindrome string, which literally means, an irritable fear of palindromes.<\/p>\r\n A palindrome phrase is just another collection of letters+numbers which reads exactly as its reversed version.<\/p>\r\n nmb = int(input(“Give a number to check if palindrome:\\n”))<\/p>\r\n tmp = nmb<\/p>\r\n revsd = 0<\/p>\r\n while (nmb > 0):<\/p>\r\n digit = nmb% 10<\/p>\r\n rev = rev * 10 + digit<\/p>\r\n nmb= nmb\/\/ 10<\/p>\r\n if (tmp == revsd):<\/p>\r\n print(“The number is palindrome!”)<\/p>\r\n else:<\/p>\r\n print(“Not a palindrome!”)<\/p>\r\n Give a number to check if palindrome:<\/p>\r\n 1231<\/p>\r\n Not a palindrome!<\/p>\r\n<\/div>\r\n def Palindrome(string):<\/p>\r\n # Run loop from 0 to half of the input<\/p>\r\n for i in range(0, int(len(string) \/ 2)):<\/p>\r\n if string[i] != str[len(string) – i – 1]:<\/p>\r\n return False<\/p>\r\n return True<\/p>\r\n s = input(“Give me your word:\\n”)<\/p>\r\n if Palindrome(s):<\/p>\r\n print(“It is A Palindrome”)<\/p>\r\n else:<\/p>\r\n print(“It is not a Palindrome”)<\/p>\r\n Give me your word:<\/p>\r\n madam<\/p>\r\n It is A Palindrome<\/p>\r\n<\/div>\r\nPalindrome<\/h3>\r\n
<\/div>\r\nPalindrome Number<\/h3>\r\n
Palindrome String<\/h3>\r\n
Program 1: Python Program To Verify A Palindrome Number<\/h3>\r\n
Algorithm:<\/strong><\/h3>\r\n
\r\n
Output:<\/strong><\/h3>\r\n
Program 2: Python Program To Verify A Palindrome String
Algorithm:<\/h3>\r\n\r\n
\r\n
Output:<\/strong><\/h3>\r\n
Program 3: Python Program To Verify Input As PalindromeAnd now, let’s solve this problem in a more pythonic way.<\/h3>\r\n
Algorithm:<\/h3>\r\n
\r\n
\r\n