Courseiva
StringshardMultiple ChoiceObjective-mapped

PCAP Strings Practice Question

A developer is tasked with validating user input that must be a 10-digit phone number. The input may contain spaces, dashes, and parentheses. Which approach best ensures the input contains exactly 10 digits?

⚠ Common exam trap

Python Institute often tests the distinction between checking if a string *contains* a certain number of digits versus checking if the string *itself* is entirely composed of digits, leading candidates to mistakenly choose options that require the entire string to be numeric.

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

if len([c for c in s if c.isdigit()]) == 10:

Uses a list comprehension to filter only digit characters from the input string `s` and then checks if the count of those digits is exactly 10. This correctly handles any non-digit characters (spaces, dashes, parentheses) by ignoring them, ensuring the validation focuses solely on the presence of exactly ten digits.

Answer analysis

Option-by-option breakdown

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

  • if len([c for c in s if c.isdigit()]) == 10:

    Why this is correct

    This expression builds a list containing only the digit characters from the input and then compares its length to 10. It therefore passes any string that contains exactly ten digits, regardless of additional letters, spaces, hyphens, or punctuation, because non-digits are simply filtered out before counting. This precisely matches the requirement to validate that user input contains ten digits without insisting on a specific format.

  • if len(s) >= 10 and s.isdigit():

    Why it's wrong here

    's.isdigit()' returns True only when every character in the string is a digit, so applying it after checking len(s) >= 10 rejects any input containing hyphens, spaces, or other separators even if those inputs contain ten digits. Furthermore, the >= operator incorrectly accepts a string of eleven or more digits, because it validates length with at least rather than exactly, allowing strings that do not have precisely ten digits.

  • if s[:10].isdigit():

    Why it's wrong here

    This option slices off the first ten characters and calls .isdigit() on that substring only, never inspecting any characters after index 9. As a result, a string that begins with ten digits but contains letters or symbols later would pass, and a string with fewer than ten characters that are all digits would also pass because slicing returns the whole string when it is shorter than the slice bound.

  • if s.isdigit() and len(s) == 10:

    Why it's wrong here

    Here the logic first demands that the entire string consists solely of digits, then demands its total length be exactly 10. That combination only matches a ten-character string with no separators, so inputs like '123-456-7890' or '123 456 7890' fail even though they contain exactly ten digits; the requirement silently enforces both a character count and an all-digit constraint, which is more restrictive than merely counting embedded digits.

About these practice questions

This PCAP question is part of Courseiva's 169-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam 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.