Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python String isalnum() Function

String in Python has built-in functions for almost every action to be performed on a string. Python String isalnum() function checks for the alphanumeric characters in a string and returns True only if the string consists of alphanumeric characters i.e. either alphabet (a-z, A-Z) or numbers (0-9) or combination of both.

Key Points :

  • Return Type: Boolean i.e. True or False
  • Parametric Values: No parameters need to be parsed in isalnum() function
  • No spaces should be present in the string
  • The empty string also returns False
  • Not case sensitive i.e. return value does not depend on the case of string

String isalnum() Syntax

str_name.isalnum()

str_name here refers to the input string. And, isalnum() is inbuilt string function in python.

str_name = "Hello123"
print(str_name.isalnum())  # True

String isalnum() Examples

Examples of different cases are given below:

Case 1: String contains only alphabets

str_name = "Hello"
print(str_name.isalnum())  # True

Case 2: String contains only numbers

str_name = "786"
print(str_name.isalnum())  # True

Case 3: String contains spaces in between

str_name = "Welcome user 123"
print(str_name.isalnum())       #False

Case 4: String contains Numbers and Alphabets with different cases

str_name = "w3lC0Me"
print(str_name.isalnum())  # True

Case 5: String contains special characters

str_name = "Hello@123"
print(str_name.isalnum())  # False

Case 6: String is empty or contains whitespaces

str_name = ' '
print(str_name.isalnum())  # False

Program to Print List of All Possible Alphanumeric Characters in Python

The Unicode module can be used to check the alphanumeric character. The program is to print all alphanumeric Unicode characters.

import unicodedata

total_count = 0
for i in range(2 ** 16):
    charac = chr(i)
    if charac.isalnum():
        print(u'{:04x}: {} ({})'.format(i, charac, unicodedata.name(charac, 'UNNAMED')))
        total_count = total_count + 1
print("Total Count of Alphanumeric Characters = ",total_count)

Output:

Output All Alphanumeric Characters
Output All Alphanumeric Characters

It is just a glance of output as actual output is lengthy. There are 49167 alphanumeric characters in all.

References:

Leave a Reply

Your email address will not be published. Required fields are marked *