How to Check if a Value is Number in Python?

How to Check if a Value is Number in Python?

In Python, you can check if a value is a number or a string contains a number using a variety of methods. For example, you can use the isinstance() function to check if a value is an instance of the int or float data types. You can also use the isdigit() and isnumeric() methods to check if a string contains only digits or numeric characters, respectively. To check if a string contains a float value, you can use the float() function and catch the ValueError exception that is raised if the string cannot be converted to a float. In this tutorial, we will demonstrate how to use these methods to check if a value is a number or a string contains a number in Python.

Check if a Value is Number in Python Examples

To check if a value is a number in Python, you can use the isinstance() function. Here is an example:

def is_number(value):
    if isinstance(value, (int, float)):
        return True
    else:
        return False

print(is_number(5))  # prints True
print(is_number(5.5))  # prints True
print(is_number('5.5'))  # prints False

In this example, the is_number() function takes a value as an argument and checks if it is an instance of either the int or float data types using the isinstance() function. If it is, the function returns True, otherwise it returns False.

The isinstance() function takes two arguments: the value to be checked and the type (or tuple of types) to check against. In this case, we are checking if the value is an instance of either int or float. If it is, isinstance() returns True, and the is_number() function returns True. If the value is not an instance of either int or float, isinstance() returns False, and the is_number() function returns False.

Check if a String Contains only Numbers in Python

To check if a string contains only numbers in Python, you can use the isdigit() method. This method returns True if all the characters in the string are digits, and False if any character is not a digit.

Here is an example:

string = '12345'
if string.isdigit():
    print('The string contains only digits.')
else:
    print('The string contains non-digits.')

Output:

The string contains only digits.

You can also use the str.isnumeric() method, which returns True if all the characters in the string are numeric characters, and False if any character is not a numeric character. Numeric characters include digits (0-9), as well as certain other characters, such as the minus sign (-) and the plus sign (+).

Here is an example using str.isnumeric():

string = '12345'
if string.isnumeric():
    print('The string contains only numeric characters.')
else:
    print('The string contains non-numeric characters.')

Output:

The string contains only numeric characters.

You can also use a combination of isdigit() and isnumeric() to check if a string contains only digits, as well as other numeric characters.

string = '12345'
if string.isdigit() and string.isnumeric():
    print('The string contains only digits.')
else:
    print('The string contains non-digits.')

Output:

The string contains only digits.

Check if String Contains Integers and Float only in Python

To check if a string contains a float value in Python, you can use the float() function and catch the ValueError exception that is raised if the string cannot be converted to a float.

Here is an example:

def is_float(string):
    try:
        float(string)
        return True
    except ValueError:
        return False

print(is_float('5.5'))  # prints True
print(is_float('5'))  # prints True
print(is_float('a'))  # prints False

In this example, the is_float() function takes a string as an argument and tries to convert it to a float using the float() function. If the string can be converted to a float, the function returns True. If the float() function raises a ValueError exception, the function catches the exception and returns False.

You can also use the isdecimal() method to check if a string contains a decimal number. The isdecimal() method returns True if all the characters in the string are decimal digits (0-9), and False if any character is not a decimal digit.

Here is an example using isdecimal():

def is_float(string):
    if string.isdecimal():
        return True
    try:
        float(string)
        return True
    except ValueError:
        return False

print(is_float('5.5'))  # prints True
print(is_float('5'))  # prints True
print(is_float('a'))  # prints False

This example first checks if the string contains only decimal digits using isdecimal(). If it does, the function returns True. If the string does not contain only decimal digits, the function tries to convert the string to a float and returns True if the conversion is successful, or False if a ValueError exception is raised.

Related: