Last modified: Feb 23, 2026 By Alexander Williams

Check if Character is Number in Python

Working with text is common in programming. You often need to check characters. A frequent task is to see if a character is a number.

Python provides simple built-in methods for this. This guide explains how to do it. We will cover the main methods and their differences.

Why Check for Numeric Characters?

You might need to validate user input. For example, checking a phone number or a PIN code. Data cleaning often requires separating numbers from letters.

Parsing files or processing text strings also needs this. Knowing how to identify numeric characters is a fundamental skill for any Python developer.

Using the isdigit() Method

The str.isdigit() method is the most common way. It returns True if all characters in the string are digits. Otherwise, it returns False.

Digits include characters for numbers 0-9. It also includes superscripts and subscripts. For a single character, you apply it to a one-character string.


# Example 1: Using isdigit() with single characters
char1 = '5'
char2 = 'a'
char3 = '²'  # Superscript 2

print(f"Is '{char1}' a digit? {char1.isdigit()}")
print(f"Is '{char2}' a digit? {char2.isdigit()}")
print(f"Is '{char3}' a digit? {char3.isdigit()}")
    

Is '5' a digit? True
Is 'a' a digit? False
Is '²' a digit? True
    

Notice that the superscript '²' returns True. This is a key point about isdigit(). It recognizes digit-like symbols beyond 0-9.

Using the isnumeric() Method

The str.isnumeric() method has a broader scope. It returns True for all digit characters. It also returns True for numeric characters from other languages.

This includes fractions, Roman numerals, and Chinese/Japanese numerals. It is the most inclusive check.


# Example 2: Using isnumeric() with various characters
char4 = '7'
char5 = '⅗'  # Fraction three-fifths
char6 = '一千'  # Chinese numeral for one thousand

print(f"Is '{char4}' numeric? {char4.isnumeric()}")
print(f"Is '{char5}' numeric? {char5.isnumeric()}")
print(f"Is '{char6}' numeric? {char6.isnumeric()}")
    

Is '7' numeric? True
Is '⅗' numeric? True
Is '一千' numeric? True
    

If your application needs to handle international numeric systems, isnumeric() is the best choice. For more on how Python handles different characters, see our Python Character Encoding Guide for Beginners.

Using the isdecimal() Method

The str.isdecimal() method is the strictest. It only returns True for characters that form numbers in base 10. These are the standard digits 0 through 9.

It will return False for superscripts, fractions, or Roman numerals. Use this when you need to check for standard Arabic numerals only.


# Example 3: Using isdecimal() for strict checking
char7 = '9'
char8 = '½'
char9 = '٥'  # Arabic-Indic digit five

print(f"Is '{char7}' decimal? {char7.isdecimal()}")
print(f"Is '{char8}' decimal? {char8.isdecimal()}")
print(f"Is '{char9}' decimal? {char9.isdecimal()}")
    

Is '9' decimal? True
Is '½' decimal? False
Is '٥' decimal? True
    

Note that the Arabic-Indic digit '٥' is considered a decimal digit. This is important for processing text from different locales.

Key Differences and When to Use Each

Understanding the difference between these methods is crucial.

  • isdigit(): Digits and digit-like symbols (e.g., ⁴).
  • isnumeric(): All numeric characters, including fractions and Chinese numerals.
  • isdecimal(): Only characters for the numbers 0-9 in various scripts.

For most basic validation (like a PIN), use isdecimal() or isdigit(). For parsing rich text, you might need isnumeric().

Practical Example: Validating a User Input String

Let's create a function that checks if every character in a string is a standard number. This is useful for password or ID validation.


def contains_only_digits(input_string):
    """
    Checks if all characters in the string are standard digits (0-9).
    Returns True if yes, False otherwise.
    """
    for char in input_string:
        if not char.isdecimal():  # Strict check for 0-9
            return False
    return True

# Test the function
test_strings = ["12345", "12a45", "987.6", "٠١٢٣"]  # Last string uses Eastern Arabic numerals

for test in test_strings:
    result = contains_only_digits(test)
    print(f"'{test}' contains only digits: {result}")
    

'12345' contains only digits: True
'12a45' contains only digits: False
'987.6' contains only digits: False
'٠١٢٣' contains only digits: True
    

The function correctly identifies strings with letters or decimals. It also accepts Eastern Arabic numerals because isdecimal() recognizes them.

Handling Edge Cases and Single Characters

Always remember these methods work on strings. To check a single character, you can use a one-character string.

What about empty strings or strings with spaces? An empty string returns False for all these checks. A space character is not a digit.


# Example 4: Edge cases
print(f"Empty string isdigit? {''.isdigit()}")
print(f"Space isdigit? {' '.isdigit()}")
print(f"Negative number? {'-5'.isdigit()}")
    

Empty string isdigit? False
Space isdigit? False
Negative number? False
    

The negative sign '-' is not a digit. You would need additional logic to handle negative numbers or decimals.

Conclusion

Checking if a character is a number in Python is straightforward. Use isdigit(), isnumeric(), or isdecimal().

Choose the method based on your needs. For standard 0-9 validation, isdecimal() is often the safest. For broader numeric symbols, use isnumeric().

These string methods are powerful tools for data validation and text processing. Incorporate them into your projects to handle user input and data cleaning effectively.