Python Forum
Palindrome.strip pirts.emordnilaP
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Palindrome.strip pirts.emordnilaP
#1
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.")
Reply
#2
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
Reply
#3
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.
Reply
#4
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.
Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug Program to check whether a number is palindrome or not PythonBoy 18 2,814 Sep-12-2023, 09:27 AM
Last Post: PythonBoy
  help with strip function gabejohnsonny21 2 2,060 May-19-2020, 04:56 AM
Last Post: gabejohnsonny21
  Palindrome checker case sensive edwdas 3 2,675 Nov-07-2019, 05:57 PM
Last Post: nilamo
  Palindrome program - I can't figure it out. Gabar112 3 3,567 Feb-20-2018, 07:03 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020