如何检查 python 中的回文?
How to check for palindromes in python?
你能写一个方法来检查一个单词或短语是否是回文吗?
注意:回文是一个反读相同的词。例如 = 女士、赛车或诸如“护士 运行”之类的短语。
你可以这样做:
def palindrome(s):
s = s.replace(' ','') # This replaces all spaces ' ' with no space ''. (Fixes issues with strings that have spaces)
return s == s[::-1] # Check through slicing
palindrome('nurses run')
你能写一个方法来检查一个单词或短语是否是回文吗?
注意:回文是一个反读相同的词。例如 = 女士、赛车或诸如“护士 运行”之类的短语。
你可以这样做:
def palindrome(s):
s = s.replace(' ','') # This replaces all spaces ' ' with no space ''. (Fixes issues with strings that have spaces)
return s == s[::-1] # Check through slicing
palindrome('nurses run')