Python Reference Library →

Python String isalnum() Method – Check Alphanumeric Strings

Let’s look at string isalnum() method – check alphanumeric strings

  1. Notice

    isalnum() tests for a nonempty string consisting entirely of Unicode letters and numeric characters.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'Ada42'
result = text.isalnum()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'Ada42'Object 2 · boolTrue
This step

isalnum() tests for a nonempty string consisting entirely of Unicode letters and numeric characters.

Picture string isalnum() method – check alphanumeric strings

isalnum() tests for a nonempty string consisting entirely of Unicode letters and numeric characters.

Image brief

Alt text
A diagram for Python String isalnum() Method – Check Alphanumeric Strings. Instructions: text = 'Ada42' result = text.isalnum() print(result). Displayed result: True. The final example bindings are text --> object 1 (str); result --> object 2 (bool). object 1: 'Ada42'; object 2: True
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String isalnum() Method – Check Alphanumeric Strings". Teaching focus: isalnum() tests for a nonempty string consisting entirely of Unicode letters and numeric characters. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'Ada42' result = text.isalnum() print(result) | v DISPLAYED RESULT True FINAL BINDINGS text --> object 1 (str) result --> object 2 (bool) object 1: 'Ada42' object 2: True Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String isalnum() Method – Check Alphanumeric Strings. Instructions: text = 'Ada42' result = text.isalnum() print(result). Displayed result: True. The final example bindings are text --> object 1 (str); result --> object 2 (bool). object 1: 'Ada42'; object 2: True

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'Ada42'
result = text.isalnum()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'Ada42'Object 2 · boolTrue
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
True

What do you expect this program to print?

What does this example display?

text = 'Ada42'
result = text.isalnum()
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

True

Follow the printed values in order. The output is: True

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

isalnum() tests for a nonempty string consisting entirely of Unicode letters and numeric characters.

isalnum() tests for a nonempty string consisting entirely of Unicode letters and numeric characters.

More examples and reference

Python String isalnum() Method

The isalnum() method in Python is used to check whether a string contains only letters and numbers (alphanumeric characters). It’s a built-in method available for string objects.

Syntax

string.isalnum()

Parameters:

  • This method does not take any parameters.

Returns:

  • True – if all characters in the string are alphanumeric (letters or digits) and there is at least one character.
  • False – if the string contains any special characters, spaces, or is empty.

Example 1: Alphanumeric String

text = "Python3"
print(text.isalnum())
True

Example 2: String with Spaces

text = "Hello World"
print(text.isalnum())
False

Example 3: String with Special Characters

text = "Python@123"
print(text.isalnum())
False

Example 4: Empty String

text = ""
print(text.isalnum())
False

Use Case: Validating Usernames

The isalnum() method is commonly used when checking for valid usernames or codes where only letters and numbers are allowed.

username = input("Enter a username: ")
if username.isalnum():
    print("Valid username.")
else:
    print("Username must be alphanumeric.")

Common Mistakes

  • Expecting isalnum() to return True for strings with spaces (it won’t).
  • Using it on an empty string – it will return False.

Summary

  • string.isalnum() checks if all characters are letters or digits.
  • Returns True only when the string has no special characters or spaces.
  • Empty strings return False.

Practice Problem

Write a program that checks whether a user’s input is a valid product code made of letters and numbers only.

product_code = input("Enter product code: ")
if product_code.isalnum():
    print("Valid code.")
else:
    print("Code should only contain letters and numbers.")

What to remember

Connect what you noticed in the example with the idea below.

Remember

isalnum() tests for a nonempty string consisting entirely of Unicode letters and numeric characters.

Watch out for this idea

isalnum() returns True for an empty string.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →