Edit 1: This article has been revised to show the differences between str.isdigit and the custom solution provided.
Edit 2: The sample also supports Unicode!

Often you will want to check if a string in Python is a number. This happens all the time, for example with user input, fetching data from a database (which may return a string), or reading a file containing numbers. Depending on what type of number you are expecting, you can use several methods. Such as parsing the string, using regex, or simply attempting to cast (convert) it to a number and see what happens. Often you will also encounter non-ASCII numbers, encoded in Unicode. These may or may not be numbers. For example ๒, which is 2 in Thai. However © is simply the copyright symbol, and is obviously not a number.

Note that if you are executing the following code in Python 2.x, you will have to declare the encoding as UTF-8/Unicode – as follows:

# -*- coding: utf-8 -*-

The following function is arguably one of the quickest and easiest methods to check if a string is a number. It supports str, and Unicode, and will work in Python 3 and Python 2.

Checking if a Python String is a Number

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        pass

    try:
        import unicodedata
        unicodedata.numeric(s)
        return True
    except (TypeError, ValueError):
        pass

    return False

When we test the function, we get the following:

>>> # Testing as a str
>>> print(is_number('foo'))
False
>>> print(is_number('1'))
True
>>> print(is_number('1.3'))
True
>>> print(is_number('-1.37'))
True
>>> print(is_number('1e3'))
True
>>>
>>> # Testing Unicode
>>> # 5 in Arabic
>>> print(is_number('٥'))
True
>>> # 2 in Thai
>>> print(is_number('๒'))
True
>>> # 4 in Japanese
>>> print(is_number('四'))
True
>>> # Copyright Symbol
>>> print(is_number('©'))
False
>>> # Testing as a str
>>> print(is_number('foo'))
False
>>> print(is_number('1'))
True
>>> print(is_number('1.3'))
True
>>> print(is_number('-1.37'))
True
>>> print(is_number('1e3'))
True
>>>
>>> # Testing Unicode
>>> # 5 in Arabic
>>> print(is_number(u'٥'))
True
>>> # 2 in Thai
>>> print(is_number(u'๒'))
True
>>> # 4 in Japanese
>>> print(is_number(u'四'))
True
>>> # Copyright Symbol
>>> print(is_number(u'©'))
False

We first try to simply convert/cast it to a float, and see what happens. This will work for the str and Unicode data types when the string is not strictly Unicode. However we also try to convert the string using the unicodedata module. What does the unicodedata.numeric function do? Well some Unicode characters have numeric attributes assigned to them, if they are a number. The unicodedata.numeric will simply return the numeric value of the character, if it exists. You may assign a default value for unicodedata.numeric to return if there is no numeric attribute, otherwise by default a ValueError will be raised.

Checking if a Python String is a Digit in Python (str.isdigit)

Python has a handy built-in function, str.isdigit, that lets you check if a string is a digit.

Numbers versus Digits

Be sure that when you use the str.isdigit function, you are really checking for a digit and not an arbitrary number. The definition of digit according to the Python documentation is as follows:

Return true if all characters in the string are digits and there is at least one character, false otherwise. Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits.
http://docs.python.org/3/library/stdtypes.html#str.isdigit

Here’s an example:

>>> print('foo'.isdigit())
False
>>> print('baz'.isdigit())
False
>>> print('1'.isdigit())
True
>>> print('36'.isdigit())
True
>>> print('1.3'.isdigit())
False
>>> print('-1.37'.isdigit())
False
>>> print('-45'.isdigit())
False
>>> print('1e3'.isdigit())
False

About The Author

  • http://dlzip.in Ashwini Chaudhary

    The isdigit() method is already available in python to check whether a String is a number or not.

    >>>str = "1234";

    >>>print str.isdigit()

    True

  • Tushar

    Ashwini,

    Check below examples, you will get the limitation of isDigit()

    str = “1234.1234″;
    print str.isdigit()
    False

    str = “1e3″
    print str.isdigit()
    False

    str = “-13″
    print str.isdigit()
    False

    Solution provided above is usefull.