Last Updated: Tuesday 17th September 2013

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:

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

When we test the function, we get the following:

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.

Here's an example:

  • 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()


  • Tushar


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

    str = “1234.1234”;
    print str.isdigit()

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

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

    Solution provided above is usefull.

  • robert.ayrapetyan@gmail.com

    Detected “is_number” function errors:

    – add “TypeError” to the first exceptions list (raised when “s” is a unicode string);
    – “return False” indentation is wrong.

  • http://laike9m.com/ laike9m


  • mayur koshti

    Very easily understandable explanation