How to Check If variable is Number in Python

When we talk about Numbers in Python, they are either integers or floats. The integers are zero, positive, or negative numbers without a fractional part. Floats represent real numbers written with a decimal point dividing the integer and fractional parts. Other than these types of values, we can not consider them as Numbers.

Python check if Number

  • Using type() function with equality operator(==).
  • Using string isnumeric() method.
  • Using numbers.Number() method.

To check if a variable is a Number in Python, use the type() function and compare its result with either int or float types to get the boolean value. If it returns True, then it is a Number. Otherwise, it is not a Number.

The type() is a built-in Python function that returns the type of the argument we pass to it.

int_num = 11
float_num = 1.9
str_num = "KB"

print(type(int_num))
print(type(float_num))
print(type(str_num))

if type(int_num) == int or type(int_num) == float:
   print("int or float")
else:
   print('The variable is not a number')

if type(float_num) == int or type(float_num) == float:
   print("int or float")
else:
   print('The variable is not a number')

if type(str_num) == int or type(str_num) == float:
   print("int or float")
else:
   print('The variable is not a number')

Output

<class 'int'>
<class 'float'>
<class 'str'>

int or float
int or float
The variable is not a number

In this example, we declared three types of variables.

  1. Integer
  2. Float
  3. String

Using the type() function, we get the data type of the variables.

Then using the if..else statement, we are comparing data type of variables with either int or float, and if it matches, that means it is a number; otherwise, they are not numbers.

Using an equality operator(==), we checked if both values are the same or not, and if it does, then it is a Number; otherwise, it is not.

Using String isnumeric() method

The string isnumeric() is a built-in method that returns True if all the characters are numeric (0-9), otherwise False. Exponents are also considered numeric values, but negative integers or float values are not considered Numbers. So, if you want to check integers, you can use this method. Otherwise, the above approach will be more helpful.

aa = "\u0040"
bb = "\u00B2"
cc = "10km2"
dd = "-19"
ee = "2.1"

print(aa.isnumeric())
print(bb.isnumeric())
print(cc.isnumeric())
print(dd.isnumeric())
print(ee.isnumeric())

Output

False
True
False
False
False

Only the second value is a numeric(0-9) value that passes the test in this example. The other values are not considered numeric by the isnumeric() function.

Using numbers.Number

To work with the numbers module, you need to import it first, and then you can use the Number class. You can test if a variable is an instance of the Number class with the isinstance() function.

import numbers

var = 5
iable = "tring"

print(isinstance(var, numbers.Number))
print(isinstance(iable, numbers.Number))

Output

True
False

As you can see that it returns True for the numeric value and False for the string.

That’s it for this tutorial.

See also

Check if a variable is None

Check if a variable is String

Check if an array is empty

Check if a dictionary has key

Leave a Comment