PCAP Strings Practice Question
A developer is building an IoT application that reads temperature data from a sensor over a TCP socket. The sensor sends data as a stream of bytes encoded in UTF-8, with each reading terminated by a newline character. The developer uses the following code to receive data:
```python
import socket
s = socket.socket() s.connect(('sensor.local', 5000)) data = s.recv(1024) ```
The variable `data` is a bytes object. The developer needs to convert it to a string to parse the temperature value. Which of the following lines of code should the developer use to correctly obtain the string representation of the received data, assuming the data is valid UTF-8 and may contain non-ASCII characters?
⚠ Common exam trap
Many exam-takers confuse `encode()` and `decode()`: candidates often think bytes need to be 'encoded' to a string, but in Python, bytes are decoded to str, and str is encoded to bytes.
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
✓
data.decode('utf-8')
The `recv()` method returns a bytes object. Since the data is valid UTF-8 and may contain non-ASCII characters, the correct way to convert bytes to a string is by calling `data.decode('utf-8')`. This method interprets the byte sequence 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.
- ✗
data.encode('utf-8')
Why it's wrong here
Calling encode('utf-8') on a bytes object is conceptually backwards and practically invalid: str objects have encode() to convert text into bytes, but bytes objects are already binary. In Python 3, bytes has no encode() method, so this line raises AttributeError instead of returning a string. Even if it existed, encoding would produce bytes, not convert bytes to text.
- ✓
data.decode('utf-8')
Why this is correct
Because data was read as raw bytes from the IoT sensor, decode('utf-8') is the correct method to interpret those bytes as a Unicode string using the UTF-8 codec. This is the inverse of str.encode() and is exactly what bytes objects are designed to do. After decoding, the result is a normal str that can be compared, parsed, or logged as text.
- ✗
bytes(data)
Why it's wrong here
The bytes() constructor, when passed an existing bytes object, simply copies the binary data into a new bytes object—it performs no decoding or text conversion. The result remains a bytes instance, so it still cannot be used as a string without a subsequent decode step. This call is therefore a useless copy operation in this context, and it leaves data in its original binary form.
- ✗
str(data)
Why it's wrong here
str(data) calls bytes.__str__, which produces a printable representation that includes a leading b and quotes, such as b'hello', and escapes any non-printable bytes. This representation is intended for debugging, not for actual text data, so it adds characters that don't exist in the original byte stream. Only decode('utf-8') yields the real string content.
Go deeper
Related to this question
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 →
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.