Ron Adam radam2 at
Fri Nov 14 12:51:02 CET 2003

On Thu, 13 Nov 2003 17:32:06 +0000, Alan Kennedy <alanmk at>

>I'm not too happy with it though. There must be some way to have a
>single fixed regular expression that can be used to test every

Thought I'd give it a try too.   This is what I came up with.  I used
the regular expression re.sub() function to remove the punctuation and

The really hard part was trying to come up with a palindrome that has
the word python in it.  :-)

_Ron Adam

Test if a string is a palindrome.
import re

def palindrome_test(p):
    p = p.lower()
    p = re.sub(r'\W','',p)
    while p:
        if p[:1] == p[-1:]:
            p = p[1:-1]
    if (len(p) <= 1):
        return True
        return False

palindrome_list = ["Bolton",
                   "No 'H' type, mate. No spot stops one tame
                   "A man, a plan, a cat, a ham, a yak, a yam, a hat,
a canal--Panama!",
                   "Was it a car or a cat I saw?",
                   "This is not a palindrome!"]

for p in palindrome_list:
    print '"'+p+'"',
    if palindrome_test(p):
        print 'is a palindrome.'
        print 'is not a palindrome.'

More information about the Python-list mailing list