A palindrome is a word, phrase, number, or other unit sequences that may be read the same way in either direction, with simple punctuation and word divider modifications. When the digits of a number are reversed, the result is the same as the original number. Palindromes can also be numerical. For instance, 1234321, madam. We’ll learn how to make a Palindrome in Python in this article.
What is Palindrome?
A palindrome is a word, phrase, number, or other sequences of units that may be read in either direction if they are separated by commas.
Happy multi-cultural palindrome day, if a little late! 02/02/2020 was a one-of-a-kind February day. It works with any date format, including MM/DD/YYYYY, DD/MM/YYYYY, and YYYY/MM/DD.
Palindromes are the name for these patterns. It makes no difference whether you read them from the first character or backward. This is an exciting beginner challenge to tackle using programming. In this article, we’ll walk through the thought process and come up with numerous strategies to determine whether the string is a palindrome or not.
A palindrome is a word, phrase, number, or other sequences of letters that reads the same forward as it does backward.
They are classified into 3 types:
What is a Palindrome Number?
When read backward, a Palindrome Number is a group of numbers that remain the same. These figures are claimed to be symmetrical as well. When the digits of a number are reversed, the result is the same as the original number. For example, the number 1234321 is a Palindrome. When the digits are reversed, the number returns to 1234321, which was our initial number. The number 1234232 isn’t a Palindrome. When the number is reversed, it becomes 2324321, which is not the same as the original.
What is a Palindrome String?
When read backward, a Palindrome String is a collection of alphabets that remain the same. Symmetrical Alphabets are another name for them. When its alphabets are written backward, the result is the exact same alphabet combination as the original string. For example, the word “madam” is a Palindrome. When the alphabets are inverted, the string returns to “madam,” which was our starting point. The word “napkin” does not have a Palindrome. When the number is reversed, it becomes “nikpan,” which is not the same as the original string.
What is the Palindrome Phrase?
When read backward, a palindrome phrase is a combination of words and special characters that stay the same. These are also known to be symmetrical sentences. When the phrase is inverted, it produces the exact same result as the original phrase. A Palindrome is, for example, a1b2c33c2b1a. If you reverse the phrase, you get a1b2c33c2b1a, which was our initial phrase. The number a4b523kg isn’t a Palindrome. When the number is inverted, it becomes gk325b4a, which is not the same as the original phrase.
Palindrome in Python Code Examples
Example 01: Using While Loop (number)
number=int(input("Enter any number :")) #store a copy of this number temp=number #calculate reverse of this number reverse_num=0 while(number>0): #extract last digit of this number digit=number%10 #append this digit in reveresed number reverse_num=reverse_num*10+digit #floor divide the number leave out the last digit from number number=number//10 #compare reverse to original number if(temp==reverse_num): print("The number is palindrome!") else: print("Not a palindrome!")
Example 02: Using While-loop (strings)
def check_palindrome(string): length = len(string) first = 0 last = length -1 status = 1 while(first<last): if(string[first]==string[last]): first=first+1 last=last-1 else: status = 0 break return int(status) string = input("Enter the string: ") status= check_palindrome(string) if(status): print("It is a palindrome ") else: print("Sorry! Try again")
Example 03: Using Reverse Function (String)
def check_palindrome_1(string): reversed_string = string[::-1] status=1 if(string!=reversed_string): status=0 return status string = input("Enter the string: ") status= check_palindrome_1(string) if(status): print("It is a palindrome ") else: print("Sorry! Try again")