Palindrome

Ron Adam radam2 at tampabay.rr.com
Fri Nov 14 13:33:50 CET 2003


On Fri, 14 Nov 2003 11:51:02 GMT, Ron Adam <radam2 at tampabay.rr.com>
wrote:

>
>"""
>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]
>        else:
>            break
>    if (len(p) <= 1):
>        return True
>    else:
>        return False


I notice right after I posted it,  I can simplify the test function a
bit more.  

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


_Ron Adam







More information about the Python-list mailing list