使用python3如何编写程序来检查文本是否属于回文(需要忽略其中的标点...

发布网友 发布时间:2022-04-23 15:48

我来回答

1个回答

热心网友 时间:2022-04-06 15:33

#!/usr/bin/python
#Filename: user_input_1.py
#Function: to check whether the string is palindrome or not. Ignore space(空格), case(大小写) and punctuation(标点符号).
#Test string: "Rise to vote,sir."

import string

def reverse(text):
return text[::-1]

def is_palindrome(text):
text = text.lower()
text = text.replace(' ', '')
for char in string.punctuation:
text = text.replace(char, '')
return text == reverse(text)

def main():
something = input('Enter text:')
if (is_palindrome(something)):
print('Yes, "{0}" is a palindrome.'.format(something))
else:
print('No, "{0}" is not a palindrome.'.format(something))

if __name__ == '__main__':
main()
else:
print('user_input_1.py was imported!')

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com