Click here to Skip to main content
15,905,232 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having difficulty figuring out how to only have letters and no punctuation
this is my code:
import re
def is_palindrome(pal):
    newStr =''
    for i in range(0,len(pal)):
        if pal[i] != ' ':
            newStr = newStr + pal[i]

    print(newStr.lower())

    
    if len(pal) <= 1:
        return True
    else:
        return pal[0] == pal[-1] and is_palindrome(pal[1:-1])

print(is_palindrome("Able was I ere I saw Elba"))
print(is_palindrome("Too bad--I hid a boot"))
print(is_palindrome("Do geese see God"))
print(is_palindrome("Murder for a jar of red rum"))
print(is_palindrome("Drab as a fool, aloof as a bard"))
print(is_palindrome("Norma is as selfless as I am, Ron"))
print(is_palindrome("Gateman sees name, garageman sees name tag"))
print(is_palindrome("A man, a plan, a canal--Panama"))
print(is_palindrome("Go deliver a dare, vile dog"))
print(is_palindrome("Go Hang a Salami! I'm a Lasagna Hog"))
print(is_palindrome("I am Happy"))
print(is_palindrome("The car went over the speed limit"))
print(is_palindrome("Coding is fun"))
print(is_palindrome("Happy go Lucky"))
print(is_palindrome("This is not a palindrome"))
print(is_palindrome("Amazon Prime Shipping"))
print(is_palindrome("A picture is worth a thousand words"))
print(is_palindrome("iPhone's are great"))
print(is_palindrome("The weather outside is nice"))
print(is_palindrome("Last one"))


What I have tried:

the output I get is:
ablewasiereisawelba
False
toobad--ihidaboot
False
dogeeseseegod
False
murderforajarofredrum
False
drabasafool,aloofasabard
False
normaisasselflessasiam,ron
False
gatemanseesname,garagemanseesnametag
False
aman,aplan,acanal--panama
False
godeliveradare,viledog
False
gohangasalami!i'malasagnahog
False
iamhappy
False
thecarwentoverthespeedlimit
False
codingisfun
False
Posted
Updated 29-Sep-21 7:43am

1 solution

You have also to apply the palindrome check to the transformed string.
Try
Python
def is_palindrome(pal):
    newpal = ''.join(ch for ch in pal if ch.isalpha()) # remove non-letters
    newpal = newpal.lower() # make lower case
    print(newpal)
    return check_palindrome(newpal)

def check_palindrome(pal):
    if len(pal) <= 1:
        return True
    else:
        return pal[0] == pal[-1] and check_palindrome(pal[1:-1])
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900