简体   繁体   中英

How to use a for loop to ignore every non-alphabetic character in a string and add to a new string? Python 3

Im trying to ignore every non-alphabetic character in a string and add the alphabetic string to a new string so it can be checked if it is a palindrome regardless of case.

So far I have

new_str = ''
for ch in s:
    ch = ch.lower
    if s.isalpha():
        s = s + new_str

return s == is_palindrome

I am calling is_palindrome prom a previous function which checks if a lowercase word is a palindrome. Any help would be greatly appreciated

There are multiple issues with your code. Let's go through them one by one:

  1. ch = ch.lower

    In order to call a function or a method, you need to use parentheses () after the function name to tell Python to actually call that function/method. The way you are doing it here, you will just assign a reference to the function to ch . So ch won't be a character afterwards. What you want to do is use ch = ch.lower() .

  2. if s.isalpha():

    This check is generally okay, however s is probably not what you mean there. s is the full string, while ch is the current character in the iteration. You probably meant if ch.isalpha() here.

  3. s = s + new_str

    Again, s is the full string which characters you are iterating through. You probably did not mean to append something to s here. You probably want to collect some result in a different variable.

    Speaking of result, new_str is initialized to the empty string '' and is never modified, so you are appending an empty string to the string s (which will not do anything).

    What you probably meant to do is modify new_str , and append the character if the earlier condition was true: new_str = new_str + ch

  4. return s == is_palindrome

    Same as before, in order to call a function, you need to use parentheses. Otherwise you would be comparing the string s with the is_palindrome function here (which is unlikely to ever be true).

    Furthermore, s is the input string (which you accidentally modified throughout the loop?). You probably meant to check new_str here.

    And finally, is_palindrome probably takes a string as an input and returns whether or not that string is a palindrome. So you wouldn't want to compare its result against the string anyway.

The full code would probably look like this after the fixes:

new_str = ''
for ch in s:
    ch = ch.lower()
    if ch.isalpha():
        new_str = new_str + ch

return is_palindrome(new_str)

Here is one way to drop all of the non-alphas from your string:

s = 'A man, a plan, a canal: Panama!'
scrunched_s = ''.join(ch.lower() for ch in s if ch.isalpha())
reversed_s = ''.join(ch.lower() for ch in s[::-1] if ch.isalpha())
print s
print scrunched_s
print reversed_s
print scrunched_s == reversed_s

Here is another:

scrunched_s = map(str.lower, filter(str.isalpha, s))
reversed_s = map(str.lower, filter(str.isalpha, reversed(s)))
print scrunched_s == reversed_s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM