Python String isdecimal() Method – Check if All Characters Are Decimals

Python String isdecimal() Method

The isdecimal() method in Python checks whether all the characters in a string are decimal characters. This method is useful when validating numerical input that should only contain digits (like "123").

Syntax

string.isdecimal()

Parameters:

  • This method takes no arguments.

Returns:

  • True – if all characters in the string are decimal characters.
  • False – if any character is not a decimal digit.

Example 1: Simple Usage

print("123".isdecimal())
True

Example 2: With Non-Decimal Characters

print("123abc".isdecimal())
False

Example 3: Empty String

print("".isdecimal())
False

What Counts as a Decimal Character?

Decimal characters are characters used to form numbers in base-10, like:

  • 0 to 9 → '0', '1', ..., '9'

It does not include:

  • Whitespace (e.g. space, tab)
  • Alphabets (e.g. 'a', 'A')
  • Special characters (e.g. '#', '@')
  • Decimal points or minus signs (e.g. '3.14', '-5')

Use Cases

  • Validating user input for numerical-only fields like age, PINs, or codes.
  • Ensuring strings contain only base-10 digit characters (unlike isdigit() which accepts other digit types too).

Comparison: isdecimal() vs isdigit() vs isnumeric()

Method Returns True For
isdecimal()Only 0-9 decimal digits
isdigit()Decimal digits + superscripts, subscripts, etc.
isnumeric()All numbers including Roman numerals, fractions, etc.

Common Mistakes

  • Using isdecimal() on an empty string returns False.
  • Floating point numbers like "3.14" will return False.
  • Negative numbers like "-10" also return False.

Interview Tip

In validation problems, use isdecimal() when you want only standard numeric digits (0-9), and not other digit representations.

Summary

  • isdecimal() checks if all characters in a string are decimal digits.
  • Returns True only for characters from '0' to '9'.
  • Does not accept floats, negatives, or whitespace.

Practice Problem

Ask the user to enter a value. Check if it is a valid positive integer using isdecimal().

user_input = input("Enter your age: ")
if user_input.isdecimal():
    print("Valid age:", user_input)
else:
    print("Please enter a valid number.")