Palindrome Program in Python
What is Palindrome Program in Python?
A Palindrome can be defined as a string or a number that displays the same value when its position is reversed. In a simpler sense, it remains unaltered when reversed.
Example: deed, 101
When the reverse word and the actual word, (a number or a string) are compared, they produce the same result.
Types of Palindrome
In Python language, Palindrome is of three types, as listed below:
- Multiple-word palindrome: This is a combination of multiple words where the value of the original and reversed words are the same.
- Single-word palindrome: This is a single word where the value of the original and reversed words are the same.
- Number palindrome: This is a number where the original and reversed numbers’ values are the same. The given input remains the same either from the forward or from the reverse side.
Algorithm for a Palindrome number in Python
The following algorithm can be applied to check the palindrome number:
Step 1) Perform variable declaration as ‘I’ and read the variable.
Step 2) Assign ‘I’ to temporary variable j. i=j
Step 3) Perform the loop with the instructions as shown below: –
- M=I % 10
- L=L x 10 + M
- I = I / 10
Step 4) Compare temporary variable with l. L==J.
Step 5) Print the number to be a palindrome if the condition is satisfied.
Step 6) If the condition does not satisfy, print the number as not palindrome.
Step 7) End
The above algorithm applies to palindrome numbers in Python.
Method 1: Palindrome number in Python using Backward Slicing
Backward slicing in Python generally involves using subset statements from the original program. Subset statements can use loops as well. Python’s palindrome program can be backward-sliced.
The following python program uses a backward loop to determine whether a string is a palindrome.
Palindrome string program in Python code:
a='deed' lena=len(a) b=[] for i in range(lena - 1, -1, -1): b.append(a[i]) rev = "".join(b) print(a + " is: ", end="") if a == rev: print("Palindrome") else: print("Not Palindrome")
Output:
deed is: Palindrome
Code Explanation:
- The Python code tries to append the empty string b in reverse order by using the length of the main string and the Python range function.
- The string b is then joined with the empty string rev using the string separator “.
Method 2: Palindrome Program in Python using Flag
Flag variables can be defined in Python to identify palindromes. Flag is initialized to 0. The following program illustrates the use of flag parameter:
Python Code for Palindrome Program:
Pld = "deed" m = -1 flag = 0 for char in Pld: if char != Pld[m]: flag = 1 break m = m - 1 print(Pld + " is: ", end="") if flag: print("Not Palindrome") else: print("Palindrome") print(flag)
Output:
deed is: Palindrome 0
Code Explanation:
- The char in Python begins with a 0 index.
- Since m is initialized as -1, the string is read from the reverse side as a negative index.
- The for loop with the if statement checks whether each character starting with index 0 present in the string matches with the reverse index of the string.
- However, if it does not match, then the program initializes the flag as 1; otherwise, it would initialize it as 0.
Method 3: Check Palindrome in Python by reversing character
The char method is used to reverse a string in this palindrome program in Python. The original string is checked with the reverse string to determine whether the string is palindrome or not.
Python Palindrome Program Code:
Base = "123" reverse = "" for char in Base: reverse = char + reverse print("Palindrome") if Base == reverse else print("Not Palindrome") print("Original string is: " + str(Base)) print("reverse string is: " + str(reverse)) Base = "101" reverse = "" for char in Base: reverse = char + reverse print("Palindrome") if Base == reverse else print("Not Palindrome") print("Original string is: " + str(Base)) print("reverse string is: " + str(reverse)) Base = "deed" reverse = "" for char in Base: reverse = char + reverse print("Palindrome") if Base == reverse else print("Not Palindrome") print("Original string is: " + str(Base)) print("reverse string is: " + str(reverse))
Output:
Not Palindrome The original string is: 123 The reverse string is: 321 Palindrome The original string is: 101 The reverse string is: 101 Palindrome The original string is: the deed The reverse string is: the deed
Code Explanation:
- Python’s str function helps convert the numerical value format to string format. The program first checks whether value 123 is a palindrome or not.
- It then checks for the value of 101 and the string value of the deed.
- The code checks for the value to be a palindrome for both numerical and string formats.
Method 4: How to check Palindrome in Python using Character Matching
In this palindrome method, the program checks whether each character in the string matches with the reverse string by using the for loop, range function, and Len function.
Python code:
def PalindromeComparator(original): for i in range(0, len(original)): if original[i] != original[len(original) - i - 1]: return False return True Base = "deed" print("Palindrome") if PalindromeComparator(Base) else print("Not Palindrome")
Output:
Not Palindrome
Code Explanation:
- A user-defined function labelled as Palindrome Comparator checks whether the value is a string or not.
- The program compares the original string and reverse of the original string by using the criteria original[i]!= original[len(original) – i – 1].
- The above program makes use of the length technique to determine the length of the string.
Method 5: Palindrome in Python using recursion
In Python, recursion means that the function calls itself repeatedly and according to a specific set of criteria. In this way, the problem can be broken down into smaller ones using the same function structure. By using recursion programming logic, palindrome strings can also be checked
Python code:
def palcomprec(number, oppo): if number == 0: return oppo remainder = int(number % 10) oppo = (oppo * 10) + remainder return palcomprec(int(number / 10), oppo) Base = 12321 reverse = 0 reverse = palcomprec(Base, reverse) print(str(Base) + " is: ", end="") print("Palindrome") if reverse == Base else print("Not Palindrome")
Output:
12321 is: Palindrome
Code Explanation:
- The program makes use of a user-defined function that recursively calls itself.
- The original number is broken down, and its residual value is added to the opposite value.
- The opposite value is multiplied with 10 first before getting added with the remainder value.
Method 6: How to use While Loop to check the Palindrome
A while loop performs the execution of code on an iterative basis until the condition applied is true.
- The iterative method can be used to check whether the integer value is palindrome.
- The Python program makes use of temporary numbers and the modulo operators.
Python code:
original = 101 temporary = original reverse = 0 while temporary > 0: remainder = temporary % 10 reverse = (reverse * 10) + remainder temporary = temporary // 10 if original == reverse: print('Palindrome') else: print("Not Palindrome")
Output:
Palindrome
Code Explanation:
- The program utilizes a while loop that checks whether the temporary number is less than zero.
- The modulo operator computes the remainder of the original number.
- The original number gets divided by 10.
- The reverse parameter is calculated by multiplying by 10.
- The resulting number is added to the residual.
- The above program checks the original number and the reverse number to classify it as palindrome or not.
Conclusion
- Palindrome is a value when compared from the backward direction, and the forward direction comes to be the same. It is used in various mathematical puzzles.
- Several ways can be adopted in Python to compare whether a value is a palindrome or not. The techniques used are: – Backward slicing, Character matching, Recursion, Use of loops, rev, Len, and str functions, pre-defined functions, recursions etc.