Hello there, I came all the way to you with the first article of #PythonShorts . This one needs to be a special one since it our beginning.
In this exercise, we will learn how to make a palindrome checker using #python .
What is a palindrome?
Any number, string or phrase which can be read in the same order both from forward and backward may be described as a palindrome. Example: madam, racecar, 1234321, etc.
Logic behind the program:
We want to make a program for checking the palindrome and for greater productivity of the program we will give freedom to the user to enter any string or number, so we will use the built - in input function.
Now for differentiating string and numbers we will use if - else boolean logic.
Then for the palindrome of numbers we will firstly separate and then organize each and every digit in a reversed order and then check it by matching with the original number saved in the temporary variable.
While, for the palindrome of the string, we will firstly reverse the whole string using slicing and indexing and then save save it in a temporary variable and then match it with the original one.
That's all. 😉
Now, the program is given below:
n= input("Enter a number or a string:") if n.isdigit(): n = int(n) s=0 a=n while a > 0: ld = a%10 s = s*10+ld a = int(a/10) if n==s: print(n," is a palindrome.") else: print(n," is not a palindrome.") else: s = n a = s[::-1] if a == s: print(n," is a palindrome.") else: print(n," is not a palindrome.") input("Press enter to exit")
Article created by: Shreejata Gupta