Python Reference Library →

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

Let’s look at string isdecimal() method – check if all characters are decimals

  1. Notice

    isdecimal() requires a nonempty string made of Unicode decimal digits. Decimal digits are narrower than all numeric characters.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = '123'
result = text.isdecimal()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'123'Object 2 · boolTrue
This step

isdecimal() requires a nonempty string made of Unicode decimal digits. Decimal digits are narrower than all numeric characters.

Picture string isdecimal() method – check if all characters are decimals

isdecimal() requires a nonempty string made of Unicode decimal digits. Decimal digits are narrower than all numeric characters.

Image brief

Alt text
A diagram for Python String isdecimal() Method – Check if All Characters Are Decimals. Instructions: text = '123' result = text.isdecimal() print(result). Displayed result: True. The final example bindings are text --> object 1 (str); result --> object 2 (bool). object 1: '123'; object 2: True
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String isdecimal() Method – Check if All Characters Are Decimals". Teaching focus: isdecimal() requires a nonempty string made of Unicode decimal digits. Decimal digits are narrower than all numeric characters. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = '123' result = text.isdecimal() print(result) | v DISPLAYED RESULT True FINAL BINDINGS text --> object 1 (str) result --> object 2 (bool) object 1: '123' object 2: True Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String isdecimal() Method – Check if All Characters Are Decimals. Instructions: text = '123' result = text.isdecimal() print(result). Displayed result: True. The final example bindings are text --> object 1 (str); result --> object 2 (bool). object 1: '123'; object 2: True

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = '123'
result = text.isdecimal()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'123'Object 2 · boolTrue
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
True

What do you expect this program to print?

What does this example display?

text = '123'
result = text.isdecimal()
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

True

Follow the printed values in order. The output is: True

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

isdecimal() requires a nonempty string made of Unicode decimal digits. Decimal digits are narrower than all numeric characters.

isdecimal() requires a nonempty string made of Unicode decimal digits. Decimal digits are narrower than all numeric characters.

More examples and reference

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.")

What to remember

Connect what you noticed in the example with the idea below.

Remember

isdecimal() requires a nonempty string made of Unicode decimal digits. Decimal digits are narrower than all numeric characters.

Watch out for this idea

isdecimal() accepts every Unicode numeric symbol.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →