1

I tried to read this as a palindrome, backward, it works within one word with no spaces but doesn't with Taco Cat.

How do I join or get rid of spaces?

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()
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Best version
  • 29
  • 1
  • 8
  • 1
    Possible duplicate of [Python remove all whitespace in a string](http://stackoverflow.com/questions/8270092/python-remove-all-whitespace-in-a-string) – Foon Oct 12 '15 at 01:55
  • I tried the does not work – Best version Oct 12 '15 at 02:04
  • Which specifically did you try? Also... string.whitespace looks like it should be a string of whitespace, so I don't see how string.whitespace() does anything besides throw an exception that you can't call a string. (https://docs.python.org/3.1/library/string.html) – Foon Oct 12 '15 at 02:13
  • tried strip(), .replace() etc – Best version Oct 12 '15 at 02:15
  • 1
    string = string.replace(' ','') works just fine for my test using "Taco Cat" – Foon Oct 12 '15 at 02:20
  • although one word works perfectly – Best version Oct 12 '15 at 02:31
  • The `string = string.replace(' ', '')` solution *does* work; if it isn't working for you, you're making another mistake somewhere. Try *replacing* your incorrect `string = string.whitespace()` call with that `string = string.replace(' ', '')` line, not just adding it to your existing code. – rmunn Oct 12 '15 at 04:06
  • Just did still not working – Best version Oct 12 '15 at 04:11
  • "still not working" tells us nothing about how to help you. WHAT is not working? What error message do you get? What results do you get, and how do they differ from what you were expecting? – rmunn Oct 12 '15 at 04:12
  • there is no error met, its converts to lover case, but not joining words – Best version Oct 12 '15 at 04:15
  • Enter a palindrome: ksk ksk ksk ksk – Best version Oct 12 '15 at 04:15
  • 1
    Because I just did *exactly* what I suggested you do (replace the `string.whitespace()` call with `string.replace(' ', '')` in your code) and it DOES work (it identified "Taco Cat" as a palindrome). So you're doing something else wrong, but we can't figure out what unless you give us more details. – rmunn Oct 12 '15 at 04:16
  • 1
    I just entered "ksk ksk ksk ksk" (without the quotes) and got "True". – rmunn Oct 12 '15 at 04:16

1 Answers1

1

string = string.replace(' ', '') can works fine here:

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()

Demo:

[user@localhost ~]$ python test.py 
Enter a palindrome: Taco Cat
True
[user@localhost ~]$ 

You also can try put a print(string) after string = string.replace(' ', '') and see what's the output. However I got tacocat.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87