Courseiva
Strings →easyMultiple Choice

PCAP Strings Practice Question

Which string method can be used to check if a string contains only digits?

⚠ Common exam trap

Python Institute often tests the subtle differences between `isdigit()`, `isdecimal()`, and `isnumeric()` by presenting a string with a Unicode digit (e.g., '²' or '½') and expecting candidates to know that `isdigit()` returns `True` for superscripts but `isdecimal()` does not, causing confusion about which method truly checks 'only digits'.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

str.isdigit()

The `str.isdigit()` method returns `True` if all characters in the string are digits (0-9) and the string is non-empty. This is the most direct and commonly used method for checking numeric-only strings in Python, as it specifically tests for digit characters without including other numeric forms like fractions or Roman numerals.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✓

    str.isdigit()

    Why this is correct

    str.isdigit() returns True only when the string is non-empty and every character has the Unicode Numeric_Type of Digit or Decimal, which includes ASCII '0'-'9', non-Arabic decimal digits, and compatibility superscripts such as '²'. This is the broadest check that still excludes letters, punctuation, and numeric fractions, so it is the correct method when the requirement is to verify that a string contains only digits.

  • ✗

    str.isalnum()

    Why it's wrong here

    str.isalnum() is wrong here because it returns True for any string composed entirely of letters and digits, meaning a value like 'abc123' passes the alphanumeric test. It only asserts that there are no punctuation, whitespace, or symbol characters; it does not require every character to be a digit. Consequently, it is far too permissive to serve as a check for digit-only content.

  • ✗

    str.isdecimal()

    Why it's wrong here

    str.isdecimal() is wrong because it is a stricter subset of str.isdigit(): it recognizes only decimal digit characters from the Unicode Nd category, such as '0'-'9' and Arabic-Indic digits, but returns False for compatibility superscripts like '²' that str.isdigit() accepts. Therefore, a string that isdigit() legitimately considers all digits can fail isdecimal(), making it unsuitable for the desired digit-only test.

  • ✗

    str.isnumeric()

    Why it's wrong here

    str.isnumeric() is wrong because it relies on a broader Unicode Numeric_Type property that classifies any numeric character as valid, including fractions like '½', Roman numerals, and other numeric symbols. For example, '½½'.isnumeric() is True even though neither character is a digit, so this method over-accepts values that are not digit-only and cannot answer the question correctly.

About these practice questions

Courseiva writes every PCAP question from scratch — 421 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.