Courseiva
Strings →easyMultiple Choice

PCAP Strings Practice Question

A programmer wants to check whether a string `s` is a palindrome (reads the same forwards and backwards, ignoring case and non-alphanumeric characters). Which code snippet correctly implements this?

⚠ Common exam trap

Python Institute often tests the candidate's understanding that a simple case-insensitive comparison is insufficient; the trap is that many candidates forget to remove non-alphanumeric characters, leading them to pick Option A or D, which only handle case but not punctuation or spaces.

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

✓

clean = ''.join(c for c in s if c.isalnum()); return clean.lower() == clean.lower()[::-1]

It first filters out non-alphanumeric characters using `c.isalnum()`, then converts the cleaned string to lowercase before comparing it with its reverse via slicing `[::-1]`. This ensures that case differences and punctuation/spaces are ignored, which is required for a proper palindrome check per the problem statement.

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.lower() == s.lower()[::-1]

    Why it's wrong here

    Lowercasing alone normalizes case but leaves spaces, punctuation, and other non-alphanumeric characters in the string. When reversed, those characters appear in different positions, so a phrase like 'A man, a plan, a canal: Panama' will not match its reverse despite being a valid palindrome after removing non-alphanumerics. Therefore, this approach fails whenever the input contains any non-alphanumeric characters.

  • ✓

    clean = ''.join(c for c in s if c.isalnum()); return clean.lower() == clean.lower()[::-1]

    Why this is correct

    This solution first constructs a cleaned string by joining only characters that satisfy isalnum(), effectively stripping spaces, punctuation, and symbols. After converting that filtered result to lowercase via .lower(), it compares it to its own reversed slice [::-1]. This correctly handles case-insensitivity and ignores all non-alphanumeric characters, making it a robust and idiomatic palindrome check.

  • ✗

    s == s[::-1]

    Why it's wrong here

    Using the raw comparison s == s[::-1] requires the original string to be exactly identical to its reverse at the character level. This means it is case-sensitive, so 'Racecar' fails because the uppercase 'R' differs from lowercase 'r', and it also includes every space and punctuation mark. It only works for strings that are already canonical lowercase, punctuation-free palindromes, which is rarely the requirement.

  • ✗

    return s.lower() == ''.join(reversed(s.lower()))

    Why it's wrong here

    This approach correctly lowercases the input and builds the reversed string by passing the lowercased sequence into reversed(), which returns a reverse iterator, and then joining the iterated characters into a new string. However, it never filters out spaces or punctuation, so any non-alphanumeric character in the original string will appear in both the forward and reversed forms but at mismatched positions. Consequently, it fails for typical palindrome phrases, just like the slicing-based version that also skips cleaning.

About these practice questions

This PCAP question is part of Courseiva's 421-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.