PCAP String slicing Practice Question
A programmer writes a function that expects a string and returns it reversed. Which code snippet correctly reverses the string 'stressed' to 'desserts'?
⚠ Common exam trap
The Python Institute often tests whether candidates know that both slice notation `[::-1]` and the combination of `reversed()` with `join()` are valid ways to reverse a string. Candidates may incorrectly think only slicing is correct or overlook that `reversed()` returns an iterator that requires `join()` to produce a string.
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
✓
result = s[::-1]
Both option B and option D correctly reverse the string 'stressed' to 'desserts'. Option B uses slice notation `[::-1]`, which creates a reversed copy of the string by stepping from end to start with a step of -1. This is the most direct and idiomatic way to reverse a string in Python. Option D uses `''.join(reversed(s))`: `reversed(s)` returns an iterator that yields characters in reverse order, and `join()` concatenates them into a new string. This is also a valid and correct approach. Option A is incorrect because strings do not have a `reversed()` method; `reversed()` is a built-in function. Option C is incorrect because `.reverse()` is a list method, not a string method, and strings are immutable.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
result = s.reversed()
Why it's wrong here
Strings do not have a `.reversed()` method; the only `reversed` available is the built-in function `reversed()`, which must be called as `reversed(s)`, not `s.reversed()`. Attempting `s.reversed()` raises an `AttributeError` because the `str` class has no such attribute. Furthermore, even the correct built-in `reversed(s)` returns an iterator, not a string, so additional processing like `''.join()` is required to obtain the reversed string—this option confuses a method call with a built-in function.
- ✓
result = s[::-1]
Why this is correct
Using extended slice syntax with a step of `-1` creates a reversed copy of the entire string: `s[::-1]` means start at the end, go to the beginning, and step backward by one. This is the most idiomatic and concise way to reverse a string in Python, and it is often preferred for its readability and speed. Since strings are immutable, this operation allocates a new string object containing the characters in reverse order, leaving the original string unchanged.
- ✗
s.reverse()
Why it's wrong here
Calling `.reverse()` on a string raises an `AttributeError` because strings are immutable sequence types and do not expose this method. The `.reverse()` method is defined only on mutable list objects, where it reverses the list in place and returns `None`, meaning it cannot produce a new reversed string. Even if strings had such a method, it could not modify the original object, so this option is fundamentally incompatible with string semantics.
- ✓
result = ''.join(reversed(s))
Why this is correct
This is correct: `reversed(s)` is a built-in function that returns a reverse iterator over the characters of `s`, and `''.join()` consumes that iterator, concatenating each character with an empty separator to form a new string. This approach works for any finite sequence, not just strings, and it is memory-efficient because the iterator yields characters lazily rather than constructing an intermediate list. The resulting expression is explicit and clear, making it a reliable way to reverse a string.
Go deeper
Related to this question
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 →
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.