Python Forum
Help with stripping special char and spaces
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with stripping special char and spaces
#1
Hi,
New to coding in my first class. Catching on but have no clue how to attack this problem I have now.
I am tasked with The user should be prompted to enter a word, phrase, sentence or number into an input string
TIP: Use a repetition loop (for) to go through each letter of the input, e.g.:
for xyz in myString: will loop through each character of the myString variable. You can then examine the character in a conditional structures (use complex Booleans – and/or) to determine if the character is a letter or a number, concatenate it to two strings (one forwards, one backwards) while removing the case sensitivity (upper or lower methods). Then compare the two strings to determine if they are equivalent and report out (print) accordingly. You may not use any features not yet covered in this course, such as lists, tuples, functions.

What I have so far is
print("Assignment 09 Palindrome Tester\n")
print("""This program accepts a word, phrase, sentence, or number
from the user and determines whether it is a palindrome or not.
A palindrome would read the same backwards as forwards, ignoring
any spaces or puncuation and is case-sensitive.  A few examples:
   -  Bob
   -  1234321
   -  Madam, I'm Adam!
   -  Was It A Rat I Saw?
   -  Mr. Owl Ate My Metal Worm\n""")

pal = 2
while pal != "0":
    pal = input("Enter a work, phrase, sentence, or number (or 0 to end) :")
    if  pal != "0":
        originalPal = str(pal)
        reversedPal = ""
        for x in range(0,len(originalPal)):
            reversedPal = originalPal[x] + reversedPal
        if originalPal.lower() == reversedPal.lower():
            print("Yes, {0} is indeed a palidrome\n".format(reversedPal))
        else:
            print("No, {0} is not a palidrome\n".format(pal))
    else:
            print("Good Bye!")
Everything works so far but I have no clue how to approach stripping out the special char and blank spaces in the user input statement. The professor gave hints:

In a loop, make a new phrase that removes all spaces and punctuation.
It should keep only A-Z and 0-9 (make case insensitive)

You can create a reversed string at the same time as removing the spaces/punctuation (just concatenate the new character in front of the current state of the reversed string.

I'm not sure who to only use the A-Z and 0-9 without using functions.

Here is my output
Enter a work, phrase, sentence, or number (or 0 to end) :racecar
Yes, racecar is indeed a palidrome

Enter a work, phrase, sentence, or number (or 0 to end) :mike
No, mike is not a palidrome

Enter a work, phrase, sentence, or number (or 0 to end) :0
Good Bye!
>>>

As posted I don't know where to begin using a loop to strip backspaces and special characters from the user input
Reply


Messages In This Thread
Help with stripping special char and spaces - by mumstead - Jun-10-2020, 09:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Use range to add char. to empty string johneven 5 13,916 Mar-24-2020, 11:52 AM
Last Post: mbharatkumar

Forum Jump:

User Panel Messages

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