Remember
isdigit() recognizes Unicode digits, including some characters such as superscripts that isdecimal() excludes.
isdigit() recognizes Unicode digits, including some characters such as superscripts that isdecimal() excludes.
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 = '123'
result = text.isdigit()
print(result)Top-level scope
'123'Object 2 · boolTrueisdigit() recognizes Unicode digits, including some characters such as superscripts that isdecimal() excludes.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = '123'
result = text.isdigit()
print(result)
|
v
DISPLAYED RESULT
True
FINAL BINDINGS
text --> object 1 (str)
result --> object 2 (bool)
object 1: '123'
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 = '123'
result = text.isdigit()
print(result)Top-level scope
'123'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 = '123'
result = text.isdigit()
print(result)isdigit() MethodThe isdigit() method in Python checks whether all characters in a string are digits (0–9). It's a handy way to verify if a string represents a number.
string.isdigit()
True – if all characters in the string are digits.False – if the string has one or more non-digit characters.text = "12345"
print(text.isdigit())
True
text = "123abc"
print(text.isdigit())
False
text = ""
print(text.isdigit())
False
The isdigit() method is commonly used to validate input from users. For example, when asking for age or mobile number:
age = input("Enter your age: ")
if age.isdigit():
print("Valid age!")
else:
print("Please enter digits only.")
Enter your age: 25
Valid age!
False)Falseisnumeric() – similar to isdigit() but includes Unicode numeric charactersisdecimal() – even stricter than isdigit()Use isdigit() to filter numeric strings during input validation, form parsing, or data cleaning.
isdigit() returns True if all characters are digits (0–9)False for empty strings, decimals, or non-digit charactersWrite a program that asks for a PIN code and prints "Valid PIN" only if all characters are digits.
pin = input("Enter your 4-digit PIN: ")
if pin.isdigit() and len(pin) == 4:
print("Valid PIN")
else:
print("Invalid PIN. Please enter a 4-digit number.")
Connect what you noticed in the example with the idea below.
isdigit() recognizes Unicode digits, including some characters such as superscripts that isdecimal() excludes.
isdigit() and isdecimal() are identical for every string.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.