Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = 'Ada42'
result = text.isalnum()
print(result)Top-level scope
'Ada42'Object 2 · boolTrueisalnum() tests for a nonempty string consisting entirely of Unicode letters and numeric characters.
(no output)
What do you expect this program to print?
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: TrueChoose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = 'Ada42'
result = text.isalnum()
print(result)Top-level scope
'Ada42'Object 2 · boolTrueThe example has finished. Compare the final output with the values and references above.
True
What do you expect this program to print?
text = 'Ada42'
result = text.isalnum()
print(result)isalnum() MethodThe 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.
string.isalnum()
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.text = "Python3"
print(text.isalnum())
True
text = "Hello World"
print(text.isalnum())
False
text = "Python@123"
print(text.isalnum())
False
text = ""
print(text.isalnum())
False
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.")
isalnum() to return True for strings with spaces (it won’t).string.isalnum() checks if all characters are letters or digits.True only when the string has no special characters or spaces.False.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.")
Connect what you noticed in the example with the idea below.
isalnum() tests for a nonempty string consisting entirely of Unicode letters and numeric characters.
isalnum() returns True for an empty string.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.