📅  最后修改于: 2020-10-29 01:24:56             🧑  作者: Mango
回文是指即使数字和字母倒置也保持不变的数字或字母。
121、11、414、1221、74747是回文数。
MOM,DAD,MADAM,REFER是回文字母。
JAVATPOINT,PROGRAM,JAVA不是回文字母。
程序1:回文字符串
str = 'JaVaJ'
str = str.casefold()
# This string is reverse.
rev = reversed(str)
if list(str) == list(rev):
print("PALINDROME !")
else:
print("NOT PALINDROME !")
输出:
PALINDROME !
程序2:回文字符串程序
string=input(("Enter a letter:"))
if(string==string[::-1]):
print("The letter is a palindrome")
else:
print("The letter is not a palindrome")
输出:
Enter a letter: javatpoint
The letter is not a palindrome
Enter a letter: MADAM
The letter is a palindrome
程序3:使用while循环的回文数程序
Num = int(input("Enter a value:"))
Temp = num
Rev = 0
while(num > 0):
dig = num % 10
rev = rev * 10 + dig
num = num // 10
if(temp == rev):
print("This value is a palindrome number!")
else:
print("This value is not a palindrome number!")
输出:
Enter the value: 2551
This value is not a palindrome number!
Enter the value: 1221
This value is a palindrome number!