Python 去掉空格或连接

Python 去掉空格或连接,python,python-3.x,Python,Python 3.x,我试着把它理解为回文,倒转,它在一个单词内没有空格,但在tacocat中不起作用 如何加入或删除空格 def is_palindrome(): string = input("Enter a palindrome: ") string = string.lower() string = string.whitespace() rev_str = reversed(string) if list(string) == list(rev_str):

我试着把它理解为回文,倒转,它在一个单词内没有空格,但在
tacocat
中不起作用

如何加入或删除空格

def is_palindrome():
    string = input("Enter a palindrome: ")
    string = string.lower()
    string = string.whitespace()
    rev_str = reversed(string)
    if list(string) == list(rev_str):
        print("True")
    else:
        print("False")

is_palindrome()

string=string.replace(“”,”)
在这里可以正常工作:

def is_palindrome():
    string = str(input("Enter a palindrome: "))
    string = string.lower()
    string = string.replace(' ', '')
    rev_str = reversed(string)
    if list(string) == list(rev_str):
        print("True")
    else:
        print("False")

is_palindrome()
演示:


您还可以尝试在
string=string之后放置
print(string)
。替换(“”,)
,然后查看输出结果。然而,我得到了
tacocate

可能的副本,我尝试了不工作字符串=string.replace('','')对于我使用“tacocat”的测试来说效果很好,因为我刚刚完全按照我的建议做了(将
string.whitespace()
调用替换为
string.replace('',)
,在代码中),它确实有效(它将“塔可猫”识别为一个回文)。所以你在做其他错事,但除非你提供更多细节,否则我们无法找出原因。我刚刚输入了“ksk ksk ksk ksk”(不带引号)并得到了“True”。
[user@localhost ~]$ python test.py 
Enter a palindrome: Taco Cat
True
[user@localhost ~]$