Python Forum

Full Version: Palindrome.strip pirts.emordnilaP
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a homework assignment that wants me to get a user input string, reverse it, then remove all punctuation. After that, I must compare original string to reversed string and output if the initial input is or is not a palindrome.

I have gotten it for the most part (I think). The problem is, I can only find the strip() method when we have not even covered it. In fact, we haven't covered anything on removing punctuation. So I ask of you, "Will you point me in the right direction so that I can remove all punctuation without strip() or any other devices that I have not covered?"

My research has put me at a dead-end. I need your help. I am not sure what to do next. Truth is, I think I should strip it before I reverse it, but honestly, I don't think I can use strip() at all. My head hurts. Please help!

# Palindrome (Program 3)

# string to test for myprogramminglab
# A Man, A Plan, A Canal: Panama

# Get user input for string
wordPhrase = input("Enter a word or phrase: \n")

# Make input lowercase
wordPhrase = wordPhrase.lower()

# Reverse input
revWord = ""
for ch in wordPhrase:
    revWord = ch + revWord

# strip punctuation from revWord
strIpped = [l.strip(':') for l in revWord]

# if/else for "is" or "is not" a palindrome
if wordPhrase == revWord:
    print(wordPhrase + " is a palindrome.")

else:
    print(wordPhrase + " is not a palindrome.")
you found strip on your own.
I think (hope) that your prof will accept that.
the other option is regular expressions which are more complex (yet easier when learned) than strip
Well, then go with it I shall. I must ask this then, should I strip first, then flip? Logically this makes sense, but putting it into play I am getting output with a : no matter which logic I use.

My current thought on the process:

---Get user Str input
---Remove Punct
---Reverse stripped string
---Compare input Str to reversed Str via
---if else loop for "is" / "is not" Palindrome
---output whichever from the loop applies

If my logic could be better please advise. I think for the most part I have it, I need to figure out how to get rid of that colon (:) in my output, though.

And thank you. I truly appreciate your help.
I always use regular expression
import re

remjunk = re.compile("[^\w']")
newstr = remjunk.sub(string)
maybe you can use this as well as it only takes a simple google to find.
(Oct-07-2017, 09:27 PM)Larz60+ Wrote: [ -> ]I always use regular expression
import re

remjunk = re.compile("[^\w']")
newstr = remjunk.sub(string)
maybe you can use this as well as it only takes a simple google to find.

This is Week 3 in Intro to Programming. I have never seen any of that. I duckduckgo'd it and found a for char in str that I am working into my code now. This just might do the trick. I should have taken this course in a classroom, not online. It is like I am teaching myself some nights. Cool Then again, learning it like this is likely teaching me well. I guess time will tell.

EDIT:
I got it!!! The for char in str worked and I lined up all of the logic and output and BAM, good to go! I uploaded it into the system and it kicked back on every run of the code. You just don't even know, Larz60+... THANK YOU!!! Man, oh man, I am so happy right now. LSU beat Florida this afternoon and now this... WOO-HOO! What a day.