Courseiva
StringseasyMultiple ChoiceObjective-mapped

PCAP Strings Practice Question

A developer needs to check if a string contains only alphanumeric characters. Which string method should be used?

⚠ Common exam trap

Watch out — candidates often confuse `isalnum()` with `isalpha()` or `isdigit()`, mistakenly thinking that checking for letters only or digits only is sufficient, when the question explicitly requires both letters and digits (alphanumeric).

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

s.isalnum()

The `isalnum()` method returns `True` if all characters in the string are alphanumeric (letters or digits) and the string is non-empty. This directly matches the requirement to check for only alphanumeric characters, covering both letters and digits without any other characters.

Answer analysis

Option-by-option breakdown

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

  • s.isnumeric()

    Why it's wrong here

    s.isnumeric() is too broad for the stated goal because it returns True for any string in which every character has a Unicode numeric value, including fractions like '½', superscripts like '²', and Roman numerals. It does not consider letters at all, so a purely alphabetic string such as 'abc' would return False, and even a mixed alphanumeric string fails. The requirement is to accept both letters and digits, so this method rejects valid letter-containing strings and is therefore incorrect.

  • s.isalnum()

    Why this is correct

    s.isalnum() exactly implements the required test: it returns True only for non-empty strings where every character is a Unicode letter or digit, accepting both 'hello123' and accented letters like 'café'. It also recognizes Unicode digits such as '١' while correctly rejecting spaces, punctuation, and symbol characters like '#' or '!'. Because the condition is precisely that the string contains only alphanumeric characters, this is the correct method and also implies that isalpha() or isdigit() would be too restrictive individually.

  • s.isdigit()

    Why it's wrong here

    s.isdigit() is narrower than the requirement because it only accepts characters classified as Unicode digits (primarily the decimal digits and superscript digits like '²'), and it returns False for fraction characters like '½' that isnumeric() would accept. It does not accept any alphabetic letters, so a string such as 'user2' with a letter and a digit evaluates to False, even though it is alphanumeric. Since the question asks about alphanumeric content rather than pure digit content, this method is an incorrect choice.

  • s.isalpha()

    Why it's wrong here

    s.isalpha() checks only for Unicode alphabetic characters and returns True only if every character is a letter, so it immediately returns False for any digit character anywhere in the string. For example, 'abc' is True but 'abc123' is False, despite being alphanumeric. It also excludes punctuation, whitespace, and symbols, but the key flaw here is that digits, which are explicitly permitted in an alphanumeric string, cause this method to fail. Therefore, it cannot determine whether a string contains only alphanumeric characters.

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.