The isnumeric()
method checks if all the characters in the string are numeric.
Example
pin = "523"
# checks if every character of pin is numeric
print(pin.isnumeric())
# Output: True
isnumeric() Syntax
The syntax of the isnumeric()
method is:
string.isnumeric()
Here, isnumeric()
checks if all characters in string
are numeric or not.
isnumeric() Parameters
The isnumeric()
method doesn't take any parameters.
isnumeric() Return Value
The isnumeric()
method returns:
True
-if all characters in the string are numericFalse
-if at least one character is not a numeric
Example 1: Python isnumeric()
symbol_number = "012345"
# returns True as symbol_number has all numeric characters
print(symbol_number.isnumeric())
text = "Python3"
# returns False as every character of text is not numeric
print(text.isnumeric())
Output
True False
In the above example, we have used the isnumeric()
method to check whether every character in symbol_number and text is numeric or not.
The method returns:
-
True
- for symbol_number since every character in"012345"
are numeric -
False
- for text since every character in"Python3"
are not numeric
Example 2: isnumeric() with Other Numeric Types
Python treats mathematical characters like numbers, subscripts, superscripts, and characters having Unicode numeric value properties (like a fraction, roman numerals, currency numerators) as numeric characters.
The isnumeric()
method returns True
with these characters. For example:
# string with superscript
superscript_string = '²3455'
print(superscript_string.isnumeric())
# string with fraction value
fraction_string = '½123'
print(fraction_string.isnumeric())
Output
True True
Here, we have used the isnumeric()
method with strings that contain superscript and fraction.
superscript_string.isnumeric()
-returnsTrue
because'²3455'
contains all numeric characters.fraction_string.isnumeric()
- returnsTrue
because'½123'
contains all numeric characters.
Also Read: