Kodeclik Blog
Python’s isnumeric() method
Python has many useful functions and methods that make a programmer’s life easier. The isnumeric() method is one such method. Consider the following piece of code:
str1 = "HumptyDumpty"
str2 = "314"
print(str1.isnumeric())
print(str2.isnumeric())
This outputs:
False
True
The first string “HumptyDumpty” is not comprised of numerical characters so the first print statement returns False as expected. The second string, even though it is a string, is comprised of only numbers and thus the second print statement returns True.
Let us make a small modification to the second string:
str1 = "HumptyDumpty"
str2 = "3.14"
print(str1.isnumeric())
print(str2.isnumeric())
This returns:
False
False
Even though “3.14” can be considered to be a floating point number, the isnumeric() method applied on this string returns False because it contains a period. isnumeric() returns True only if the string strictly contains characters 0..9 and certain other unicode characters.
Using isnumeric() on Unicode strings
What is unicode? Unicode is a character alphabet that aims to capture every character used across multiple human languages and notations. For instance, the four-digit code “0041” refers to uppercase A. So you can print “A” by printing the unicode code 0041 (you tell Python that you mean unicode notation using the prefix “\u”). (Here, 0041 is the representation of A in hexadecimal notation.)
Here is a program that demonstrates this concept:
print('\\u0041')
print('0041')
This outputs:
A
0041
Unicode allows you to introduce mathematical notation such as fractions and squares. For instance, the unicode character “\u00B2” denotes a number squared so that “8\u00B2” denotes 8 squared, or 64. Let us try this out.
print('8\\u00B2')
This will output:
8²
You can verify that Python can interpret this in a numerical context by typing:
print('8\\u00B2')
print('8\\u00B2'.isnumeric())
The output is:
8²
True
Similarly, the unicode character “\u2079” denotes a number raised to the power of 9. So the following piece of code:
print('8\\u2079')
print('8\\u2079'.isnumeric())
yields:
8⁹
True
Let us try a fractional unicode representation.
print('8\\u2155')
print('8\\u2155'.isnumeric())
The result is:
8⅕
True
isnumeric() also recognizes roman characters. For instance:
print('\\u2168')
print('\\u2168'.isnumeric())
returns:
Ⅸ
True
Finally, isnumeric() recognizes certain currency numerators in multiple languages.
isnumeric() versus isdigit()
isdigit() is just like isnumeric() but it only recognizes numbers, superscripts, and subscripts. It doesn’t recognize fractions, roman numerals, and currency numerators. For instance:
print('\\u2168')
print('\\u2168'.isnumeric())
print('\\u2168'.isdigit())
returns:
Ⅸ
True
False
isnumeric() versus isdecimal()
isdecimal() is even more restrictive than isdigit(). It only recognizes numbers but does not recognize superscripts and subscripts.
For instance, the following program:
print('8\\u2079')
print('8\\u2079'.isnumeric())
print('8\\u2079'.isdigit())
print('8\\u2079'.isdecimal())
outputs:
8⁹
True
True
False
Note that none of the above functions recognize decimal points. Thus the following program:
print('8.14')
print('8.14'.isnumeric())
print('8.14'.isdigit())
print('8.14'.isdecimal())
outputs:
8.14
False
False
False
When to use isnumeric()
isnumeric() is very useful to read strings, e.g., from a user’s input or from a file, and see if the input can be interpreted in a numeric context. If it returns True, then you can choose to do special processing that is specific to numbers.
A simple program using isnumeric()
Let us take two numbers as input from the user and print their sum. We will first use isnumeric() to check if the numbers given by the user can indeed be added:
x1 = input("Enter the first number: ")
x2 = input("Enter the second number: ")
if (x1.isnumeric() and x2.isnumeric()):
print("I can add these!")
else:
print("I cannot add these - sorry!")
If we give as inputs “2” and “a”, this will return:
Enter the first number: 2
Enter the second number: a
I cannot add these - sorry!
If we give as inputs “3” and “5”, this will return:
Enter the first number: 3
Enter the second number: 5
I can add these!
Once you have verified in this manner, you can proceed to convert the string to integer (e.g., by a casting function such as int()) and then to add them (this is left as an exercise to the reader).
In this blogpost, we have learnt the very useful Python isnumeric() method that helps check if a string of characters can be interpreted in a numeric context.
Interested in more things Python? See our blogpost on
Python's if not operator and Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.