Python String isnumeric()
Method
The isnumeric() method in Python checks whether all characters in a string are numeric. It returns True
if all characters are numbers, and False
otherwise.
Syntax
string.isnumeric()
Parameters:
- No parameters are required.
Returns:
True
– if all characters in the string are numericFalse
– if at least one character is not numeric
Example 1: Basic Usage
text = "12345"
print(text.isnumeric())
True
Example 2: String with Letters
text = "123abc"
print(text.isnumeric())
False
Example 3: Unicode Numeric Characters
text = "²³"
print(text.isnumeric())
True
These are superscript characters which are still considered numeric in Unicode.
Use Cases
- Validate if a string is a numeric ID or code
- Input validation for number-only fields
- Useful in form handling and data cleaning
Difference Between isnumeric()
, isdigit()
, and isdecimal()
Method | Supports Digits | Supports Unicode Numbers | Supports Decimal Characters |
---|---|---|---|
isnumeric() |
✅ | (e.g., ½ , ² ) |
✅ |
isdigit() |
✅ | (less than isnumeric) | ✅ |
isdecimal() |
✅ | ❌ | (only 0–9) |
Common Mistakes
- Using
isnumeric()
on strings with spaces or punctuation returnsFalse
- Strings with decimal points like
"3.14"
are not numeric by this method
Interview Tip
isnumeric()
is often useful when filtering user inputs or validating data in string format.
Summary
isnumeric()
returnsTrue
if all characters in the string are numeric.- Supports Unicode digits and numeric characters like superscripts and fractions.
- Does not recognize decimal points or negative signs as numeric.
Practice Problem
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.")