Python Reference Library →

Python String isdigit() Method – Check If a String Has Only Digits

Let’s look at string isdigit() method – check if a string has only digits

  1. Notice

    isdigit() recognizes Unicode digits, including some characters such as superscripts that isdecimal() excludes.

  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.isdigit()
print(result)
Live state

Top-level scope

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

isdigit() recognizes Unicode digits, including some characters such as superscripts that isdecimal() excludes.

Picture string isdigit() method – check if a string has only digits

isdigit() recognizes Unicode digits, including some characters such as superscripts that isdecimal() excludes.

Image brief

Alt text
A diagram for Python String isdigit() Method – Check If a String Has Only Digits. Instructions: text = '123' result = text.isdigit() 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 isdigit() Method – Check If a String Has Only Digits". Teaching focus: isdigit() recognizes Unicode digits, including some characters such as superscripts that isdecimal() excludes. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = '123' result = text.isdigit() 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 isdigit() Method – Check If a String Has Only Digits. Instructions: text = '123' result = text.isdigit() 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.isdigit()
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.isdigit()
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

isdigit() recognizes Unicode digits, including some characters such as superscripts that isdecimal() excludes.

isdigit() recognizes Unicode digits, including some characters such as superscripts that isdecimal() excludes.

More examples and reference

Python String isdigit() Method

The isdigit() method in Python checks whether all characters in a string are digits (0–9). It's a handy way to verify if a string represents a number.

Syntax

string.isdigit()

Parameters:

  • This method does not take any parameters.

Returns:

  • True – if all characters in the string are digits.
  • False – if the string has one or more non-digit characters.

Example 1: Basic Usage

text = "12345"
print(text.isdigit())
True

Example 2: Contains Letters

text = "123abc"
print(text.isdigit())
False

Example 3: Empty String

text = ""
print(text.isdigit())
False

Use Case: Validating User Input

The isdigit() method is commonly used to validate input from users. For example, when asking for age or mobile number:

age = input("Enter your age: ")
if age.isdigit():
    print("Valid age!")
else:
    print("Please enter digits only.")

Output Example:

Enter your age: 25
Valid age!

Common Mistakes

  • Does not allow negative numbers (e.g., "-123" → False)
  • Decimal numbers (e.g., "3.14") also return False

Related Methods

  • isnumeric() – similar to isdigit() but includes Unicode numeric characters
  • isdecimal() – even stricter than isdigit()

Interview Tip

Use isdigit() to filter numeric strings during input validation, form parsing, or data cleaning.

Summary

  • isdigit() returns True if all characters are digits (0–9)
  • Returns False for empty strings, decimals, or non-digit characters
  • Commonly used in input validation

Practice Problem

Write a program that asks for a PIN code and prints "Valid PIN" only if all characters are digits.

pin = input("Enter your 4-digit PIN: ")
if pin.isdigit() and len(pin) == 4:
    print("Valid PIN")
else:
    print("Invalid PIN. Please enter a 4-digit number.")

What to remember

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

Remember

isdigit() recognizes Unicode digits, including some characters such as superscripts that isdecimal() excludes.

Watch out for this idea

isdigit() and isdecimal() are identical for every string.

This is the misconception to avoid.

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

    Reference library →