Remember
isnumeric() accepts a nonempty string of Unicode numeric characters, including some fractions and numerals.
isnumeric() accepts a nonempty string of Unicode numeric characters, including some fractions and numerals.
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 = '½'
result = text.isnumeric()
print(result)Top-level scope
'½'Object 2 · boolTrueisnumeric() accepts a nonempty string of Unicode numeric characters, including some fractions and numerals.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = '½'
result = text.isnumeric()
print(result)
|
v
DISPLAYED RESULT
True
FINAL BINDINGS
text --> object 1 (str)
result --> object 2 (bool)
object 1: '½'
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 = '½'
result = text.isnumeric()
print(result)Top-level scope
'½'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 = '½'
result = text.isnumeric()
print(result)isnumeric() MethodThe isnumeric() method in Python checks whether all characters in a string are numeric. It returns True if all characters are numbers, and False otherwise.
string.isnumeric()
True – if all characters in the string are numericFalse – if at least one character is not numerictext = "12345"
print(text.isnumeric())
True
text = "123abc"
print(text.isnumeric())
False
text = "²³"
print(text.isnumeric())
True
These are superscript characters which are still considered numeric in Unicode.
| Method | Supports Digits | Supports Unicode Numbers | Supports Decimal Characters |
|---|---|---|---|
isnumeric() |
✅ | (e.g., ½, ²) |
✅ |
isdigit() |
✅ | (less than isnumeric) | ✅ |
isdecimal() |
✅ | ❌ | (only 0–9) |
isnumeric() on strings with spaces or punctuation returns False"3.14" are not numeric by this methodisnumeric() is often useful when filtering user inputs or validating data in string format.
isnumeric() returns True if all characters in the string are numeric.Write a program that asks the user to enter a student roll number and verifies if it's numeric:
roll = input("Enter student roll number: ")
if roll.isnumeric():
print("Valid roll number.")
else:
print("Invalid! Roll number must be numeric.")
Connect what you noticed in the example with the idea below.
isnumeric() accepts a nonempty string of Unicode numeric characters, including some fractions and numerals.
isnumeric() accepts the decimal point in 3.5.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.