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.
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 or Python Training in Bangalore at FITA.
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.
Such patterns are palindrome. The palindrome is defined as the words, numbers or phrases that form mirror images on reversal.
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.
The string or the word aibohphobia is an example of a palindrome string, which literally means, an irritable fear of palindromes.
A palindrome phrase is just another collection of letters+numbers which reads exactly as its reversed version.
Program 1: Python Program To Verify A Palindrome Number
- Take the input and store in a variable.
- Use a temporary variable with the same input value for reserve.
- Use a while loop to store each digit (using modulus) and reverse it (multiply by 10 and add the removed digit to the current reversed number).
- Remove the last digit from the input.
- The loop terminates when the input is reduced to zero or becomes zero.
- The reversed number is then compared with the input value stored in the temporary variable.
- If both are equal, then the input is a palindrome number, else it is not.
- Print the final result.
nmb = int(input(“Give a number to check if palindrome:\n”))
tmp = nmb
revsd = 0
while (nmb > 0):
digit = nmb% 10
rev = rev * 10 + digit
nmb= nmb// 10
if (tmp == revsd):
print(“The number is palindrome!”)
print(“Not a palindrome!”)
Give a number to check if palindrome:
Not a palindrome!
Program 2: Python Program To Verify A Palindrome String
- Define a function with an argument.
- Run a for loop, from 0 to the length of the input divided by 2.
- Note that if the string has an odd number of characters the middlemost term will be ignored.
- Compare the first with last, second with second from the last and so on.
- If any compare does not match then it is not a palindrome, and return False.
- Return true if all of the comparisons match.
# Run loop from 0 to half of the input
for i in range(0, int(len(string) / 2)):
if string[i] != str[len(string) – i – 1]:
s = input(“Give me your word:\n”)
print(“It is A Palindrome”)
print(“It is not a Palindrome”)
Give me your word:
It is A Palindrome
Program 3: Python Program To Verify Input As PalindromeAnd now, let’s solve this problem in a more pythonic way.
- Take input as either a word or number.
- Note that the number will be taken as a string using the input method.
- Reverse it using the string slicing method.
- Print the statement based on the equality of the reversed and the original word or number.
string = input(‘Enter word or number:\n’)
print(‘A Palindrome’ if string == string[::-1] else “Not a palindrome!”)
Enter word or number:
Not a Palindrome!
You can also try to solve this problem using the built-in reversed and join function of Python…
To get in-depth knowledge of Python along with its various applications, you can enroll for live Python Online Training with certification, support and career guidance.