Python Forum

Full Version: Help with stripping special char and spaces
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
it specifically states
Quote: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 have used
for x in range(0,len(originalPal))
see [Basic] Never use "for i in range(len(sequence)):"

It's difficult to know how to help because we don't know what you can and cant use.
Quote:You may not use any features not yet covered in this course, such as lists, tuples, functions.

You can create a string of acceptable characters and check if each character is in the string, but I don't know if this is how you are expected to do it

The following is no needed
originalPal = str(pal)
as input returns a str

Tips
myString = 'this'
for char in myString:
    print(char)
Output:
t h i s
acceptable = 'ABCD'
print('A' in acceptable)
print('£' in acceptable)
Output:
True False
myString = 'this'
print(f'before {myString}')
print(f'{myString} after')
Output:
before this this after
I thought about using
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
But this doesn't check for blank spaces if My name is Mike is entered by the user
I'm thinking I can use if elif else and use \b to backspace if any punctuations are found
Does that make any sense?

Basically we are just learing loops no functions allowed I have googled and see strip() would work but I can't use that
I would make it as simple as only add a character to the forward/backward version of the string if it is an acceptable character.
Sorry not following what you mean there.
Using the tips in my first post
loop through to get each character
use upper or lower on the character
check if the character is acceptable
build a forward and backward version of acceptable characters only
compare them.
OK so I have my notacceptable variable and can't figure out how to make 2 new strings from my user input that will remove notacceptable characters from pal and reversedPal to make strings go from My name is Mike to MynameisMike so I can then compare the strings.

print("""Assignment 09 Palindrome Tester
Developed by Mike Umstead at SMCC for CIS156

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 punctuation 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) :")
    acceptable = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890'
    notacceptable = ""
    for char in pal:
        if char not in acceptable:
            notacceptable = char
            if  pal != "0":
            reversedPal = ""
        for x in range(0,len(pal)):
            reversedPal = pal[x] + reversedPal
            if pal.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!")
I will give some more tips:
  • There is only one for loop required
  • create an empty string variable forward and backwards before the for loop
  • You only need to list either the upper case or lower case letters if you change each character to upper or lower first in the for loop
  • after changing the case of character check if the char is in the acceptable string
  • add the acceptable character at the end of forwards and the beginning of backwards
  • compare forwards and backwards