- 回文:顺读和倒读都一样的字符串
根据“回文”的解释来做这道题:用一个列表保存字符串的逆序输入,然后跟原来的字符串转换成的列表做对比。
代码如下:
#判断一个字符串是不是“回文”
def is_palindrome(s):
lists = list(s)
l = list(s)
new_list = []
for i in range(len(lists)):
new_list.append(lists.pop())
if new_list == l:
return True
else:
return False
print(is_palindrome("abcdefgfedcba"))
运行结果如下:
True
Process finished with exit code 0