Courseiva
StringshardMultiple ChoiceObjective-mapped

PCAP Strings Practice Question

A Python script reads a file containing text with non-ASCII characters like 'é' and 'ü'. The script must encode the string as UTF-8 then decode it back. Which of the following correctly handles this without error?

⚠ Common exam trap

Python Institute often tests the distinction between string and bytes methods — the trap here is that candidates confuse `.encode()` and `.decode()`, thinking both can be called on strings, or they incorrectly assume ASCII can handle non-ASCII characters without error.

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

s.encode('utf-8').decode('utf-8')

It first encodes the string (which contains non-ASCII characters like 'é' and 'ü') into UTF-8 bytes using `.encode('utf-8')`, then decodes those bytes back into a string using `.decode('utf-8')`. This round-trip preserves all characters since UTF-8 can represent any Unicode code point, and the operations are applied in the correct order: a string is encoded to bytes, then bytes are decoded back to a 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.

  • s.decode('utf-8').encode('utf-8')

    Why it's wrong here

    In Python 3, a text string like s is a str object that has an encode() method, while decode() exists only on bytes objects. Calling s.decode('utf-8') treats the str as if it were a bytes buffer, which is a type error and raises AttributeError: 'str' object has no attribute 'decode'. Consequently, the subsequent .encode('utf-8') is never reached. This option mistakenly applies a byte-level operation to already-decoded text.

  • s.encode('ascii').decode('ascii')

    Why it's wrong here

    Encoding s with ASCII will fail for any character whose code point exceeds U+007F, such as accented letters or CJK characters that are precisely the non-ASCII content the script is reading. The encode('ascii') call raises UnicodeEncodeError before .decode('ascii') ever executes. Even if the text happened to be pure ASCII, this round-trip is technically valid but defeats the purpose; the correct codec must be capable of representing the full Unicode range, and UTF-8 is the standard choice.

  • s.encode('utf-8').decode('utf-8')

    Why this is correct

    This is the correct round-trip: s.encode('utf-8') serializes the Unicode string into a bytes object using UTF-8's variable-length encoding, and .decode('utf-8') deserializes those exact bytes back into the original str. Because UTF-8 can encode every Unicode code point, the transformation is lossless and the resulting string is equal to s. Unlike the wrong options, it respects the proper direction (str → bytes → str) and never applies decode to a str. This pattern is commonly used when passing text through byte-oriented APIs or verifying byte-level round-trippability.

  • s.decode('utf-8').decode('utf-8')

    Why it's wrong here

    Calling s.decode('utf-8') is invalid because s is already a str and decode is not a method of str; attempting it raises AttributeError immediately. Had it succeeded (which it cannot), the result would still be a str, so calling decode a second time would repeat the same invalid operation — decoding is only for bytes, and there are no bytes left after a hypothetical first decode. This option stacks two type errors, whereas the correct sequence would require encoding to bytes first. It confuses the direction of the codec bridge.

About these practice questions

Courseiva writes every PCAP question from scratch — 169 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 →

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.