0

I have the user input strings until they are finished at which point they input Done. I then check each string to see if it is a palindrome or not. If the string is a palindrome then, I then insert it into a list. I have my palindrome checking code working for strings like "swap paws" but it does not work for strings like "taco cat". I can't include libraries to help me with this so I'm unsure as to how I can go about ignoring spaces and case. This isn't the same as other questions asked here because the ones that do talk about ignoring space and case use libraries and the others just talk about checking if a basic string with no spaces or anything special is a palindrome. Here is my code:

plist={}
val=1
print("Enter the strings: ")

inp = raw_input()       # Get the input
if(inp==inp[::-1]):
    plist[inp] = val

while inp != "Done":        # Loop until Done is entered
    if(inp==inp[::-1]):    #  inp==inp[::-1]
        plist[inp] = val
    inp = raw_input()   # Get the input again

print("The palindromes are: ")
print(plist)
ParPro
  • 197
  • 1
  • 6
  • 16
  • Possible duplicate of [How to check for palindrome using Python logic](https://stackoverflow.com/questions/17331290/how-to-check-for-palindrome-using-python-logic) – William Perron May 30 '18 at 15:47
  • you should check the standard library for [`str`](https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str) you'll find there how to remove spaces and change case – bobrobbob May 30 '18 at 15:50

2 Answers2

1

This seems to be about filtering out whitespace characters, the palindrome stuff you have already figured out.

To filter out a space character you can do this:

>>> "".join([c for c in "taco cat" if c != " "])
'tacocat'

For other whitespace characters you can change the if filter:

... c not in [" ", "\t", "\n", ...]
Phil
  • 160
  • 13
0

Here you have the answer you may be looking for:

(this program runs on Python 3 - maybe some functions are different in Python 2 or lower)

# Reverse of a string
def reverse(input_string: str):
    return input_string[::-1]

# Main function
def is_palindrome(input_string: str):
    # type: () -> bool
    reverse_string = reverse(input_string)
    return reverse_string == input_string

# Now your program
palindrome_words = []
is_program_finished = False
do:
    input_text = input("Put your palindrome here or type \"Done\" for finish: ")
    if input_text != "Done":
        if is_palindrome(input_text):
            palindrome_words.append(input_text)
    else:
        is_program_finished = True
while not is_program_finished
# I recommend you to use "pprint" for displaying list
from pprint import pprint
pprint(palindrome_words)
# Else:
print(palindrome_words)

Hope it helps ^^

Javinator9889
  • 16
  • 1
  • 2