7-3 sdut-判断回文字符串

知识点

正则表达式

代码1 使用sub

import re
str=input()
# 转化为小写
str=str.lower()
# 只保留数字和字母
str=re.sub('[^0-9a-z]',"",str)
# 逆转字符串
str1=str[::-1]
if str==str1:
    print('yes')
else : print('no')

代码2 使用findall

import re
str=input()
str=str.lower()
str=re.findall("[0-9a-zA-Z]",str)
cnt=str[::-1]
if str==cnt:
    print("yes")
else :
    print("no")
posted @ 2022-06-07 16:01  kingwzun  阅读(112)  评论(0编辑  收藏  举报