发布网友 发布时间:2022-04-18 20:07
共2个回答
懂视网 时间:2022-04-19 00:29
回文即顺着读和倒着读都一样的字符串。#设置需要过虑的标点符号 forbidden = (".", "?", "!", ":", ";", "-", "—", "()", "[]", "...", "'", '""', "/", ",", " ") #获取一个字符串,书中要求确认"Rise to vote, sir."是回文 text = input("请输入:") #将字符串倒过来 def reverse(text): str_tmp = [] str = "" for i in range(0,len(text)): if text[i] in forbidden: continue else: str_tmp.append(text[i].lower())#方便比较,将字母转成小写字母 return str.join(str_tmp)[::-1] #做是否是回文检测 def is_palindrome(text): str_tmp = [] str = "" for i in range(0,len(text)): if text[i] in forbidden: continue else: str_tmp.append(text[i].lower()) return str.join(str_tmp) == reverse(text) #输出检测结果 if is_palindrome(text): print(text, "是回文") else: print(text, "不是回文")
热心网友 时间:2022-04-18 21:37
参考资料 A Byte of Python