PCAP Strings Practice Question
A programmer writes a function to check if a string is a palindrome (ignoring case and non-alphanumeric characters). Which implementation correctly achieves this?
⚠ Common exam trap
Python Institute often tests the requirement to ignore non-alphanumeric characters and case, and the trap here is that candidates may forget to filter the string before reversing, leading them to choose options A or D which only handle case but not punctuation.
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
✓
def is_pal(s): s = ''.join(c for c in s if c.isalnum()).lower(); return s == s[::-1]
It first filters the string to keep only alphanumeric characters using `c.isalnum()`, converts the result to lowercase with `.lower()`, and then compares the string to its reverse using slicing `s[::-1]`. This correctly handles case insensitivity and ignores non-alphanumeric characters, which is the standard approach for palindrome checking in Python.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
def is_pal(s): s = s.lower(); return s == ''.join(reversed(s))
Why it's wrong here
This version normalizes case but leaves all punctuation and whitespace intact, so phrases like 'A man, a plan, a canal: Panama' will not be recognized because the comma, spaces, and colon remain in the comparison. The reversal via ''.join(reversed(s)) is valid and creates a fully reversed string, but the failure stems from omitting the alphanumeric-filter step. As a result, only strings consisting entirely of letters (after lowercasing) can pass, making it unsuitable for real-world phrases.
- ✓
def is_pal(s): s = ''.join(c for c in s if c.isalnum()).lower(); return s == s[::-1]
Why this is correct
This is the correct palindrome checker because it first strips out every non-alphanumeric character with a generator expression and ''.join(), then lowercases the cleaned string. Comparing this normalized form to its extended-slice reverse s[::-1] accounts for spaces, punctuation, and mixed case in one clean pass. This is the robust, idiomatic approach expected for a general-purpose palindrome test.
- ✗
def is_pal(s): return s == s[::-1]
Why it's wrong here
This bare version performs no normalization whatsoever, so it treats the input as raw characters and compares it to its reverse exactly. Common palindrome inputs like 'Racecar' fail due to the leading uppercase 'R', and phrases with spaces or punctuation are never recognized unless the caller has already sanitized them. It may work only for pre-lowercased, single-word strings with no punctuation, making it a fragile and incomplete solution.
- ✗
def is_pal(s): s = s.lower(); return s == s[::-1]
Why it's wrong here
This version applies .lower() to disregard case but then compares the still-unfiltered string against its reverse using slicing. It will reject classic palindrome phrases such as 'never odd or even' because spaces are retained and cause the forward and reversed strings to differ. The slicing reversal is efficient, but the missing alphanumeric filter is the critical flaw that keeps this from being a correct general palindrome checker.
Go deeper
Related to this question
About these practice questions
One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
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.