Python判断回文串

2023-04-17 00:00:00 python 判断 回文

方法1:使用切片

def is_palindrome_1(s):
return s == s[::-1]

方法2:使用循环

def is_palindrome_2(s):
for i in range(len(s)//2):
if s[i] != s[-i-1]:
return False
return True

测试

s1 = "pidancode.com"
s2 = "皮蛋编程"

print(is_palindrome_1(s1)) # False
print(is_palindrome_1(s2)) # True

print(is_palindrome_2(s1)) # False
print(is_palindrome_2(s2)) # True

相关文章