How do you find a non alphanumeric character in python?

There are multiple problems with this code.

First, if True: is always true, so the "Nope" will always happen, and if False: is never true, so none of that stuff will ever happen. I think you wanted this:

if pwd[count].isalnum[]:
    print "Nope"
else:
    print "Good job"
    count = count + 1
    number += 1

Also, I think you want to increment count always, not just if it's a symbol, so:

if pwd[count].isalnum[]:
    print "Nope"
else:
    print "Good job"
    number += 1
count = count + 1

Meanwhile, you need some kind of loop if you want this to happen over and over. For example:

while count < len[pwd]:
    if pwd[count].isalnum[]:
        # etc.

However, you really don't need to maintain count yourself and keep doing pwd[count]; you can use a for loop for this:

for ch in pwd:
    if ch.isalnum[]:
         # etc.

Meanwhile, while you do return a value from the end of the function, you don't do anything with that returned value when you call the function. What you need is:

number = non_alnum_2[total, pwd]

Also, there's no reason to pass total to non_alnum_2 here. In fact, it doesn't do anything useful at all.

So, putting it all together:

def non_alnum_2[pwd]:
    lid = 3
    number = 0
    for ch in pwd:
        if ch.isalnum[]:
            print "Nope"
        else:
            print "Good job"
            number += 1
    if number > lid:
        number = lid
    return number

pwd = raw_input["What is your password? "]

number = non_alnum_2[pwd]
print number

In this article, we show how to extract only non-alphanumeric characters from a string in Python using regular expressions.

So, say, we have the string, "The Knicks game yesterday was great!!! The Knicks won 112-92 at MSG"

And we just want to extract the non-alphanumeric characters. This is characters that are neither a number nor an alphabetical character. We can do this in Python with a basic regular expression.

We simply write a regular expression that only allows non-alphanumeric characters to be returned. Any other characters will not be returned.

The regular expression statement that only returns non-alphanumeric characters is shown below.

This regular expression above will only have non-alphanumeric characters returned. It will not return any other type of character.

To get the full picture, let's look at a complete example.

This is shown in the code below.

So the first thing is that in order to use regular expressions in Python, you have to import the re module. So this is the first thing we do in our code above.

Next, we have the phrase that we extract from, "The Knicks game yesterday was great!!! The Knicks won 112-92 at MSG"

Our goal is to write a regular expression that gets all non-alphanumeric characters from this string.

The expression to do so is, patterns= [r'\W+']

Next, when you're using a regular expression to match a pattern in a string, you must use a for loop for the pattern that you create.

The reason for this is that patterns checks multiple instances of the string. Therefore, it is not just checking the entire string, "The Knicks game yesterday was great!!! The Knicks won 112-92 at MSG" just all as a whole. It is checking every instance of the string to see if there are multiple areas in the string that matches this pattern.

We then create a variable called match and set it equal to, re.findall[p, phrase]

With this line, we are looking to see if any of the phrase has any non-alphanumeric characters.

If so, Python returns it as a list.

We then print out the result.

The result we get is shown below.

Since spaces, exclamation points, and hyphens are non-alphanumeric characters, these are returned as output.

We can also create the code with a function, so that we can just call the function and return the results.

The code above returns the same output as the previous code.

So this is simple code of how to extract non-alphanumeric characters from a string in Python using regular expressions.

Related Resources

This post will discuss how to remove non-alphanumeric characters from a string in Python.

1. Using regular expressions

A simple solution is to use regular expressions for removing non-alphanumeric characters from a string. The idea is to use the special character \W, which matches any character which is not a word character.

importre

if__name__=='__main__':

    input="Welcome, User_12!!"

    s= re.sub[r'\W+','',input]

    print[s]    # WelcomeUser_12

Download  Run Code

 
The \W is equivalent of [^a-zA-Z0-9_], which excludes all numbers and letters along with underscores. If you need to remove underscores as well, you can do like:

importre

if__name__=='__main__':

    input="Welcome, User_12!!"

    s= re.sub[r'[^a-zA-Z0-9]','', input]

    print[s]    # WelcomeUser12

Download  Run Code

 
If the expression is used several times in a single program, you should compile and save the resulting regular expression object for reuse.

importre

if__name__=='__main__':

    input="Welcome, User_12!!"

    pattern=re.compile['\W']

    s= re.sub[pattern,'',input]

    print[s]    # WelcomeUser_12

Download  Run Code

2. Using isalnum[] function

Another option is to filter the string that matches with the isalnum[] function. It returns true if all characters in the string are alphanumeric, false otherwise.

if__name__=='__main__':

    input ="Welcome, User_12!!"

    s= ''.join[filter[str.isalnum, input]]

    print[s]    # WelcomeUser12

Download  Run Code

 
This is equivalent to:

if__name__=='__main__':

    input ="Welcome, User_12!!"

    s= ''.join[cforcin inputifc.isalnum[]]

    print[s]    # WelcomeUser12

Download  Run Code

That’s all about removing non-alphanumeric characters from a string in Python.

 
Also See:

Remove specific characters from a Python string

Remove Punctuations from a Python string

How do you find non alphabetic characters in Python?

Python isalnum That's where isalnum[] can be helpful. isalnum[] is a built-in Python function that checks whether all characters in a string are alphanumeric. In other words, isalnum[] checks whether a string contains only letters or numbers or both.

How do you know if a Python is not alphanumeric?

Python string isalnum[] function returns True if it's made of alphanumeric characters only. A character is alphanumeric if it's either an alpha or a number. If the string is empty, then isalnum[] returns False .

How do you find non alphabetic characters?

*[^a-zA-Z0-9]. *$ tests for any character other than a-z, A-Z and 0-9. Thus if it finds any character other than these, it returns true[means non-alphanumeric character].

How do you filter alphanumeric in Python?

Using isalnum[] function Another option is to filter the string that matches with the isalnum[] function. It returns true if all characters in the string are alphanumeric, false otherwise.

Chủ Đề