标签:__name__ code false 标点符号 ali UNC imp string lower
# 10-palindrome.py import string def is_palindrome(text: str) -> bool: ‘是否为回文‘ # 1、先去除标点符号以及空格,并将所有字母小写化 result = ‘‘ for i in range(len(text)): if not text[i] in string.punctuation + ‘ ‘: result += text[i].lower() print(result) # 2、判断是否为回文 n = len(result) for i in range(len(result) // 2): if result[i] != result[n-i-1]: return False return True if __name__ == ‘__main__‘: print(is_palindrome(‘I prefer pi.‘)) print(is_palindrome(‘A man, a plan, a canal: Panama.‘)) print(is_palindrome(‘Resistance is futile!‘))
标签:__name__ code false 标点符号 ali UNC imp string lower
原文地址:https://www.cnblogs.com/noonjuan/p/11407841.html