Introduction to Python in Palindrome
A Palindrome is a series of letters, as a word, or a series of numbers, which says the same word when being read both in forward and backward directions. Python Palindrome allows symbols, characters, punctuations and even spaces in the Palindrome words. It functions as a comparison between the actual word, and the reverse of the word that results in an exact match, resulting in the matched value is True. In Python, the palindrome methodology is of three different types: the single word palindrome, the multiple words palindrome, and the number palindrome.
some among the single word palindromes are listed below,
Types and Techniques Python in Palindrome
Below mentioned are the Types of Python Palindrome.
1. Single-Word Palindromes: Anna, Solos, Rotator, Radar, Sagas, Rotor, Tenet, Repaper, Civic, Kayak, Level, Madam, Racecar, Stats, Redder, Wow, Mom, Refer, Noon
2. Multiple Word Palindromes: Don’t nod, I did, did I?, My gym
3. Palindrome Numbers : 11,66,77,767,454,36763
Palindrome Program (Technique 1)
# This program performs palindrome check for a string # # function which return reverse of a string def isPalindrome(s): # Calling reverse function if len(s) <= 1 : return True if s == s[len(s) - 1] : return isPalindrome(s[1:len(s) - 1]) else : return False # Driver code Palindrome_input_Variable = [ ' AnnA ' , ' SoloS ' , ' RotatoR ' , ' RadaR ' , ' SagaS ' , ' RotoR ' , ' TenT ' , ' RepapeR ' , ' CiviC ' , ' KayaK ' , ' Lever ' , ' MadaM ' , ' RacecaR ' , ' StatS ' , ' Redder ' , ' Wow ' , ' MoM ' , ' RefeR ' , ' NooN '] print( " PALINDROME CHECK PROGRAM " ) for i in Palindrome_input_Variable: ans = isPalindrome(i) if ans == 1: print( " The given string ", "'" , i , "' ","is a palindrome") else: print( " The given string " , "'" , i , "' ","is not a palindrome")
Explanation: This program is implied to check whether the given string is a palindrome or not. Since the input is a string, this check is achieved through the python reverse function. The process flow in the ispalindrome function is as below,
Functions of Python in Palindrome
1) The reverse of the argument of the function is determined and stored in a separate variable. here the reverse is determined using the length technique. The variable’s length is determined, and a manual reverse on top of the length is applied.
2) Then, the variable with reverse stored and the actual variable is compared to check whether they hold the same value.
3) If both are matched, then the value true is returned from the function. In the case both the values don’t make a match, then the value false is returned to the function.
4) So when the value is true, then the message stating “The given string is a palindrome” is printed, instead of when it’s false, then the message stating “the given string is not a palindrome is printed.”
Palindrome Program (Technique 2)
# This program performs palindrome check for a number # # Entering the input Number = input('Enter the number to be verified : ') # Try block try: #Casting of the entered input is also achieved here by implying #variable casting process into place val = int(Number) #checking for a palindrome in the given string if Number == str(Number)[::-1]: print('The given number is PALINDROME') else: print('The given number is NOT a PALINDROME') except ValueError: print("! ! ! A valid numeric input is not entered ! ! !")
Explanation: As verified for string, the palindrome can also be checked on the numeric values. A palindrome in numeric values also means that the value and it is reverse is the same. Here based on the keyed number, the reverse of the number is generated from the pattern ” str(Number)[::-1], “. and this generated output is compared with the actual value. when the generated value is an exact reverse of the given string, then the output is printed as ” ‘ The given number is PALINDROME ‘ “. In the other case, the output is printed as ” ‘ The given number is NOT a PALINDROME ‘ “.
Palindrome Program (Technique3)
# This program performs palindrome check for a number # # Entering the input # Casting of the entered input is also achieved here by implying #variable casting process into place. num = int( input ( " ENTER THE NUMBER: " ) ) temporary = num rev = 0 # looping the given input and reversing the value while temporary != 0: rev = ( rev * 10 ) + ( temporary % 10 ) temporary = temporary // 10 if num == rev: print( " number is palindrome " ) else: print( " number is not palindrome " )
Explanation: This is also a palindrome check program on a numeric value presented. this technique involves reversing the given number using a mathematical formula, and the formula is like below,
rev = (rev * 10) + (temporary % 10)
temporary = temporary // 10
Passing the input value to this formula successfully reverses the given integer, and this generated output is compared with the actual value. when the generated value is an exact reverse of the given string, then the output is printed as ” ‘ number is palindrome ‘ “. In the other case, the output is printed as ” ‘ The given number is ” number is not palindrome ‘ “.
These programs are implied to check whether the given string is a palindrome or not. Using the above programs, any given string or a numeric value can be successfully evaluated whether they are a palindrome or not.
This has been a guide to Palindrome in Python. Here we discuss an introduction to Python Palindrome and its different techniques and types, along with an example. You can also go through our other suggested articles to learn more –