When we were younger, it was amusing to read strings backward; later, we learned that such strings, which can be read correctly either way, are called palindromes. Our keen interest led us to teach our machines to recognize palindromes, and as much as we adore other languages, none of them compare to Python. If you’re a fan of Python and enjoy coding, keep reading to find out how to make a Palindrome.
1What does palindrome mean?
A palindrome is a number or letter that remains unchanged when its digits or letters are reversed.
- The numerals 232, 77, 313, 5665, and 84847 are all examples of palindromes.
- The palindromes are the letters BOB, DAD, MADAM, and REFER.
- The palindromes are not the letters in SLAJOBS, POWER, or JAVA.
2Algorithm for Palindromes
- Please read the digit or letter.
- In a temporary variable, keep the letter or number.
- The letter or number should be reversed.
- Try comparing the temporary variable to a letter or integer that has been reversed.
- The output is instructed to Print “This number or string is a palindrome” if the two numerals or characters are matching.
- Otherwise, the output is meant to print “this number or string is not a palindrome.”
3Palindrome Program
3.1Illustration 1: Palindrome String
str = ‘JaVaJ’
str str = str.casefold()
# This string is reverse.
rev = reversed(str)
if list(str) == list(rev):
print(“PALINDROME !”)
else:
print(“NOT PALINDROME !”)
Output
Palindrome!
3.2Illustration 2: Palindrome String Program
string = input((“Enter a letter:”))
if(string==string[::-1]):
print(“The letter is a palindrome”)
else:
print(“The letter is not a palindrome”)
Output
Enter a letter: slajobs
The letter is not a palindrome
Enter a letter: LEVEL
The letter is a palindrome
3.3Illustration 3: palindrome Program using Loop
Num = int(input(“Enter a value:”))
Temp = num
Rev = 0
while(num >0):
dig = num % 10
rev rev = rev * 10 + dig
num num = num // 10
if(temp > == rev):
print(“This value is a palindrome number!”)
else:
print(“This value is not a palindrome number!”)
Output
Enter the value: 7841
This value is not a palindrome number!
Enter the value: 2002
This value is a palindrome number!
4Conclusion
This takes us to the blog’s conclusion, where we learnt how to use Python to find palindromes. All of the information presented to you in this tutorial should now be clear, I hope. Happy Learning!