1

I've been able to isolate the list (or string) of characters I want excluded from a user entered string. But I don't see how to then remove all these unwanted characters. After I do this, I think I can try joining the user string so it all becomes one alphabet input like the instructions say.

Instructions:

Remove all non-alpha characters Write a program that removes all non-alpha characters from the given input.

For example, if the input is:
-Hello, 1 world$!
the output should be:
Helloworld

My code:

userEntered = input()
makeList = userEntered.split()
def split(userEntered):
    return list(userEntered)
    
    
if userEntered.isalnum() == False:
    for i in userEntered:
        if i.isalpha() == False:
            #answer = userEntered[slice(userEntered.index(i))]
           reference = split(userEntered)
           excludeThis = i
           print(excludeThis)
 
    

When I print excludeThis, I get this as my output:

-
,
 
1
 
$
!

So I think I might be on the right track. I need to figure it out how to get these characters out of the user input. Any help is appreciated.

Henry
  • 3,472
  • 2
  • 12
  • 36
skysthelimit91
  • 103
  • 1
  • 3
  • 11
  • Before asking a question on Stack Overflow, try to do a search first. A quick search gives https://stackoverflow.com/questions/11434599/remove-list-from-list-in-python , the top answer of which should be applicable here with only a little modification. – Imperishable Night Apr 08 '22 at 00:05
  • 2
    Instead of thinking about "remove", think about creating a new string that contains only the characters you want. – Tim Roberts Apr 08 '22 at 00:08
  • 1
    Does this answer your question? [Remove list from list in Python](https://stackoverflow.com/questions/11434599/remove-list-from-list-in-python) – Imperishable Night Apr 08 '22 at 00:10
  • Why are you turning the user input into a list? The question doesn't say anything about words, it's just about characters. – Barmar Apr 08 '22 at 00:21
  • @ImperishableNight What does this have to do with lists? – Barmar Apr 08 '22 at 00:22
  • You can do this with a simple `re.sub()` call. Just create a regular expression that matches everything except alphabetic characters. This can be done using the `[...]` character set pattern. – Barmar Apr 08 '22 at 00:23
  • @Barmar Because the OP is thinking in term of lists (it's in their code as well as the question title), and I'm trying to show them that their problem can be solved with a search. – Imperishable Night Apr 08 '22 at 00:30
  • @ImperishableNight But they're thinking wrong when they think of lists. – Barmar Apr 08 '22 at 00:32
  • @Barmar I'll argue that in this case they are not wrong by that much. `''.join([char for char in userEntered if char.isalpha()])` is not *that* bad of a solution (obviously you can remove the square brackets, but that will come up naturally when the OP actually learns generator comprehension). – Imperishable Night Apr 08 '22 at 00:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243702/discussion-between-imperishable-night-and-barmar). – Imperishable Night Apr 08 '22 at 00:38

2 Answers2

1

Loop over the input string. If the character is alphabetic, add it to the result string.

userEntered = input()
result = ''
for char in userEntered:
    if char.isalpha():
        result += char
print(result)

This can also be done with a regular expression:

import re

userEntered = input()
result = re.sub(r'[^a-z]', '', userEntered, flags=re.I)

The regexp [^a-z] matches anything except an alphabetic character. The re.I flag makes it case-insensitive. These are all replaced with an empty string, which removes them.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This does bring up an interesting point since the behavior of the regexp-based solution is slightly different from `str.isalpha()`: `[a-z]` only matches alphabetic characters *in English*. Whether accented letters, Cyrillic letters, etc. should count as "alphabetic" is likely use case dependent, so I won't say the second solution is wrong, but it's different. – Imperishable Night Apr 08 '22 at 00:54
  • I guess `[^[:alpha]]` would be language-independent. I doubt the instructor cares whether this handles non-ASCII. – Barmar Apr 08 '22 at 00:56
  • Thank you. The first solution includes things I've learned so far. I should have been able to think of it. I appreciate it. – skysthelimit91 Apr 08 '22 at 01:08
1

There's basically two main parts to this: distinguish alpha from non-alpha, and get a string with only the former. If isalpha() is satisfactory for the former, then that leaves the latter. My understanding is that the solution that is considered most Pythonic would be to join a comprehension. This would like this:

''.join(char for char in userEntered if char.isalpha())

BTW, there are several places in the code where you are making it more complicated than it needs to be. In Python, you can iterate over strings, so there's no need to convert userEntered to a list. isalnum() checks whether the string is all alphanumeric, so it's rather irrelevant (alphanumeric includes digits). You shouldn't ever compare a boolean to True or False, just use the boolean. So, for instance, if i.isalpha() == False: can be simplified to just if not i.isalpha():.

Acccumulation
  • 3,491
  • 1
  • 8
  • 12
  • "BTW, there are several places in the code where you are making it more complicated than it needs to be". Yea I tend to do that. I've been having a hard time with this. Thanks for your great explanations. – skysthelimit91 Apr 08 '22 at 01:46