Remember
isdecimal() requires a nonempty string made of Unicode decimal digits. Decimal digits are narrower than all numeric characters.
isdecimal() requires a nonempty string made of Unicode decimal digits. Decimal digits are narrower than all 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 = '123'
result = text.isdecimal()
print(result)Top-level scope
'123'Object 2 · boolTrueisdecimal() requires a nonempty string made of Unicode decimal digits. Decimal digits are narrower than all numeric characters.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = '123'
result = text.isdecimal()
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.isdecimal()
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.isdecimal()
print(result)isdecimal() MethodThe isdecimal() method in Python checks whether all the characters in a string are decimal characters. This method is useful when validating numerical input that should only contain digits (like "123").
string.isdecimal()
True – if all characters in the string are decimal characters.False – if any character is not a decimal digit.print("123".isdecimal())
True
print("123abc".isdecimal())
False
print("".isdecimal())
False
Decimal characters are characters used to form numbers in base-10, like:
'0', '1', ..., '9'It does not include:
isdigit() which accepts other digit types too).| Method | Returns True For |
|---|---|
isdecimal() | Only 0-9 decimal digits |
isdigit() | Decimal digits + superscripts, subscripts, etc. |
isnumeric() | All numbers including Roman numerals, fractions, etc. |
isdecimal() on an empty string returns False."3.14" will return False."-10" also return False.In validation problems, use isdecimal() when you want only standard numeric digits (0-9), and not other digit representations.
isdecimal() checks if all characters in a string are decimal digits.True only for characters from '0' to '9'.Ask the user to enter a value. Check if it is a valid positive integer using isdecimal().
user_input = input("Enter your age: ")
if user_input.isdecimal():
print("Valid age:", user_input)
else:
print("Please enter a valid number.")
Connect what you noticed in the example with the idea below.
isdecimal() requires a nonempty string made of Unicode decimal digits. Decimal digits are narrower than all numeric characters.
isdecimal() accepts every Unicode numeric symbol.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.