This article is part of in the series
Last Updated: Wednesday 29th December 2021

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: