Home »
Python
Python String isnumeric() Method
By IncludeHelp Last updated : December 15, 2024
Python String isnumeric() Method
The isnumeric() is an in-built method in Python, which is used to check whether a string contains only numeric values or not.
Numeric contain all decimal characters and the fraction type values like (Half (½), one fourth (¼)). This method is different from isdecimal() Method because it can check other numeric values except only decimal characters (from 0 to 10).
Note
- Numeric values contains decimal characters (all digits from 0 to 9) and fractional part of the values like ½, ¼.
- String should be Unicode object - to define a string as Unicode object, we use u as prefix of the string value.
Syntax
String.isnumeric();
Parameters
None
Return Type
- true - If all characters of the string are numeric then method returns true.
- false - If any of the characters of the string is not a numeric then method returns false.
Example 1
# numeric values (decimal characters)
str1 = u"362436"
print(str1.isnumeric())
# numeric values (no decimal)
str2 = u"½¼"
print(str2.isnumeric())
# numeric values (with decimals)
str3 = u"½¼3624"
print(str3.isnumeric())
# numeric values with alphabets
str4 = u"Hello½¼3624"
print(str4.isnumeric())
Output
True
True
True
False
Example 2
# Example of Python String isnumeric() Method
str1 = "12345"
print(str1.isnumeric()) # True
str2 = "123.45"
print(str2.isnumeric()) # False
str3 = "一二三四五" # Chinese numerals
print(str3.isnumeric()) # True
str4 = "²³" # Superscript numerals
print(str4.isnumeric()) # True
str5 = "123abc"
print(str5.isnumeric()) # False
Output
True
False
True
True
False
Reference: String isnumeric()