PCAP Strings Practice Question
A web application receives a byte string b'\xc3\xa9' which represents the character 'é' in UTF-8. The developer wants to convert it to a Python string. Which operation should be used?
⚠ Common exam trap
The PCAP exam often tests the distinction between .encode() and .decode() on bytes vs. strings, trapping candidates who mistakenly use .encode() on bytes or try to decode with an incompatible codec like ASCII.
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
✓
b'\xc3\xa9'.decode('utf-8')
The byte string b'\xc3\xa9' is a UTF-8 encoded representation of the character 'é'. To convert it to a Python string, you must decode it using the .decode('utf-8') method, which interprets the bytes according to the UTF-8 encoding and returns a Unicode string.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
b'\xc3\xa9'.encode('utf-8')
Why it's wrong here
This line attempts to call .encode('utf-8') on an object that is already a bytes literal. In Python 3, .encode() is a method of str that produces bytes; bytes objects have no .encode() method, so this raises AttributeError. Even conceptually, applying an encode operation to a bytes object would try to treat binary data as text, but 0xC3 0xA9 is already UTF-8-encoded data, not a Unicode string.
- ✗
b'\xc3\xa9'.tostring()
Why it's wrong here
The bytes type does not define a tostring() method; the only way to convert bytes to a text string is through decode() or the str() constructor with an explicit encoding. Calling .tostring() would immediately raise AttributeError. Moreover, a method whose name suggests 'to string' would still need to know the character encoding of the bytes, which this call does not specify, so it could never produce the correct Unicode text.
- ✗
str(b'\xc3\xa9', 'ascii')
Why it's wrong here
This uses the str(bytes, encoding) form, which is equivalent to bytes.decode(encoding), but specifies ASCII as the codec. The bytes 0xC3 0xA9 are the UTF-8 representation of 'é'; in ASCII, bytes must be in the range 0x00-0x7F, so encountering 0xC3 immediately raises UnicodeDecodeError. Thus this option fails, and it also ignores the fact that the byte sequence is intentionally UTF-8 encoded.
- ✓
b'\xc3\xa9'.decode('utf-8')
Why this is correct
This is the correct conversion: bytes.decode('utf-8') interprets the two-byte sequence 0xC3 0xA9 as the UTF-8 encoding of the Unicode code point U+00E9, which is the character 'é'. The decode() method is specifically designed to turn bytes back into a str using a specified codec, making this the exact inverse of 'é'.encode('utf-8'). The result is the Python 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.