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:
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:
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:
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.
This will output:
You can verify that Python can interpret this in a numerical context by typing:
The output is:
Similarly, the unicode character “\u2079” denotes a number raised to the power of 9. So the following piece of code:
Let us try a fractional unicode representation.
The result is:
isnumeric() also recognizes roman characters. For instance:
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:
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:
Note that none of the above functions recognize decimal points. Thus the following program:
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:
If we give as inputs “2” and “a”, this will return:
If we give as inputs “3” and “5”, this will return:
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.
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.