Is string an alphanumeric python?

❮ String Methods


Example

Check if all the characters in the text are alphanumeric:

txt = "Company12"

x = txt.isalnum()

print(x)

Try it Yourself »


Definition and Usage

The isalnum() method returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9).

Example of characters that are not alphanumeric: (space)!#%&? etc.


Syntax

Parameter Values

No parameters.


More Examples

Example

Check if all the characters in the text is alphanumeric:

txt = "Company 12"

x = txt.isalnum()

print(x)

Try it Yourself »


❮ String Methods


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.

Is string an alphanumeric python?

Python string isalnum() example

s = 'HelloWorld2019'
print(s.isalnum())

Output: True

s = 'Hello World 2019'

print(s.isalnum())

Output: False because whitespace is not an alphanumeric character.

s = ''
print(s.isalnum())

Output: False because it’s an empty string.

s='A.B'
print(s.isalnum())

s = '10.50'
print(s.isalnum())

Output:

False
False

The string contains period (.) which is not an alphanumeric character.

s = 'çåøÉ'
print(s.isalnum())

Output: True because all these are Alpha characters. Alphabetic characters are those characters defined in the Unicode character database as “Letter”, i.e., those with general category property being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”.

Printing all Alphanumeric characters in Python

We can use unicode module to check if a character is alphanumeric or not. Here is the program to print all the alphanumeric unicode characters.

import unicodedata

count = 0
for codepoint in range(2 ** 16):
    ch = chr(codepoint)
    if ch.isalnum():
        print(u'{:04x}: {} ({})'.format(codepoint, ch, unicodedata.name(ch, 'UNNAMED')))
        count = count + 1
print(f'Total Number of Alphanumeric Unicode Characters = {count}')

Output:

...
ffd7: ᅲ (HALFWIDTH HANGUL LETTER YU)
ffda: ᅳ (HALFWIDTH HANGUL LETTER EU)
ffdb: ᅴ (HALFWIDTH HANGUL LETTER YI)
ffdc: ᅵ (HALFWIDTH HANGUL LETTER I)
Total Number of Alphanumeric Unicode Characters = 49567

I have provided only partial output because the number of alphanumeric unicode characters is huge.

You can checkout more Python examples from our GitHub Repository.

Reference: Official Documentation

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Python String isalnum() method checks whether all the characters in a given string are either alphabet or numeric (alphanumeric) characters.

    Python String isalnum() Method Syntax:

    Syntax:  string_name.isalnum() 

    Parameter:  isalnum() method takes no parameters 

    Return: 

    • True: If all the characters are alphanumeric 
    • False: If one or more characters are not alphanumeric 

    Python String isalnum() Method Example:

    Python program to demonstrate the use of String isalnum() method

    Python

    string = "abc123"

    print(string.isalnum())

    Output: 

    True

    Example 1: More examples on Python String isalnum() Method

    Python3

    string = "abc 123"

    print(string, "is alphanumeric?", string.isalnum())

    string = "abc_123"

    print(string, "is alphanumeric?", string.isalnum())

    string = "000"

    print(string, "is alphanumeric?", string.isalnum())

    string = "aaaa"

    print(string, "is alphanumeric?", string.isalnum())

    Output:

    abc 123 is alphanumeric? False
    abc_123 is alphanumeric? False
    000 is alphanumeric? True
    aaaa is alphanumeric? True

    Example 2: isalnum() in if…else Statement

    We can also use Python String isalnum() Method along with if…else statements, to output custom messages

    Python3

    password = "user123456"

    if password.isalnum():

        print("Password is alphanumeric.")

    else:

        print("Password is not alphanumeric.")

    Output:

    Password is alphanumeric.

    What is considered alphanumeric in Python?

    A character is alphanumeric if it's either an alpha or a number. If the string is empty, then isalnum() returns False .

    Are strings alphanumeric?

    An alphanumeric string is a string that contains only alphabets from a-z, A-Z and some numbers from 0-9. Explanation: This string contains all the alphabets from a-z, A-Z, and the number from 0-9. Therefore, it is an alphanumeric string.

    Is string an alphabet Python?

    Python String isalpha() The isalpha() method returns True if all characters in the string are alphabets. If not, it returns False.

    How do I check if a string contains alphanumeric characters?

    Using Regular Expression The idea is to use the regular expression ^[a-zA-Z0-9]*$ , which checks the string for alphanumeric characters. This can be done using the matches() method of the String class, which tells whether this string matches the given regular expression.