Open links in new tab
  1. To check if a string is alphanumeric in Python, you can use the isalnum() method. This method returns True if all characters in the string are alphanumeric (letters and numbers) and False otherwise.

    Example

    txt = "Company12"
    print(txt.isalnum()) # Output: True

    txt = "Company 12"
    print(txt.isalnum()) # Output: False

    Using Regular Expressions

    You can also use regular expressions to check if a string is alphanumeric. The re module provides support for regular expressions in Python.

    import re

    txt = "Company12"
    print(bool(re.match('^[a-zA-Z0-9]+$', txt))) # Output: True

    txt = "Company 12"
    print(bool(re.match('^[a-zA-Z0-9]+$', txt))) # Output: False

    Using isalpha() and isdigit()

    Another approach is to check each character individually using the isalpha() and isdigit() methods.

    def is_alphanumeric(string):
    return all(char.isalpha() or char.isdigit() for char in string)

    txt = "Company12"
    print(is_alphanumeric(txt)) # Output: True

    txt = "Company 12"
    print(is_alphanumeric(txt)) # Output: False

    These methods provide different ways to check if a string is alphanumeric, depending on your specific needs.

    Feedback
  1. Python String isalnum() Method - W3Schools

    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.

    Code sample

    txt = "Company12"
    x = txt.isalnum()
    print(x)...
  2. In Python, how do I check if a string has alphabets or numbers?

    Jul 13, 2011 · If you want to check if ALL characters are alphanumeric: string.isalnum() (as @DrTyrsa pointed out), or; bool(re.match('[a-z0-9]+$', thestring, re.IGNORECASE)) If you want to check if at least one alphanumeric character is present: import string alnum = set(string.letters …

    • Reviews: 2
    • Python String isalnum() Method - GeeksforGeeks

      4 days ago · The isalnum() method is a string function in Python that checks if all characters in the given string are alphanumeric. If every character is either a letter or a number, isalnum() returns True . Otherwise, it returns False .

    • Python String isalnum() (With Examples) - Programiz

      • The syntax of the isalnum()method is: Here, the isalnum() method checks if all the characters of stringare alphanumeric or not.
      See more on programiz.com
    • Alphanumeric Manipulation in Python: A Comprehensive Guide

      Apr 8, 2025 · This blog will explore the fundamental concepts, usage methods, common practices, and best practices related to alphanumeric operations in Python. In Python, alphanumeric characters refer to a combination of alphabetic letters (both uppercase and …

    • Check if String contains only Alphanumeric in Python

      To check if given string contains only alphanumeric characters in Python, use String.isalnum() function. The function returns True if the string contains only alphanumeric characters and False if not.

    Refresh