Valid Escape Sequences in Python Strings
Which THREE of the following escape sequences are valid in a Python string and represent a single character? (Select exactly three.)
Quick Answer
The correct answer is that \n, \', and \\ are the three valid escape sequences that each represent a single character in a Python string. These sequences are part of Python’s predefined set of escape characters, where a backslash signals that the following character should be interpreted differently—\n inserts a newline, \' allows a single quote inside a single-quoted string, and \\ produces a literal backslash. On the Certified Associate Python Programmer PCAP exam, this tests your understanding of string literals and the distinction between valid and invalid escape sequences; a common trap is assuming \x alone is valid, but it requires exactly two hexadecimal digits to form a character like \x1F. Remember that only backslash combinations recognized by Python’s parser count as a single character—anything else, like \q, is treated as a literal backslash followed by a letter. A handy mnemonic is “backslash plus n, quote, or backslash—those three are the only ones that pass the test.”
⚠ Common exam trap
The PCAP exam often tests the distinction between valid and invalid escape sequences, and the trap here is that candidates may assume any backslash-letter combination (like \q) is valid, or that \x alone is sufficient, when in fact only a fixed set of sequences are recognized and incomplete sequences cause a SyntaxError.
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
✓
\'
The backslash followed by a single quote (\') is a valid escape sequence in Python that represents a literal single quote character, allowing it to appear inside a single-quoted string without terminating the string. This sequence is interpreted as a single character by the Python parser.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
\x
Why it's wrong here
Incomplete hex escape; must be followed by two hex digits.
- ✗
\q
Why it's wrong here
Not a recognized escape sequence; would be treated as literal backslash and q.
- ✓
\'
Why this is correct
Single quote escape.
- ✓
\\
Why this is correct
Backslash escape.
- ✓
\n
Why this is correct
Newline character.
Go deeper
Related to this question
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 →
Same concept, more angles
2 more ways this is tested on PCAP
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. Which THREE of the following are valid escape sequences in Python strings?
medium- A.\g
- ✓ B.\t
- C.\h
- ✓ D.\r
- ✓ E.\n
Why B: \t is the standard escape sequence for a horizontal tab character in Python strings. Escape sequences in Python begin with a backslash followed by a specific character, and \t is defined in the Python language specification (similar to C) to represent the ASCII tab character (0x09).
Variation 2. Which THREE are valid escape sequences in Python strings?
hard- ✓ A.\n
- B.\q
- ✓ C.\\
- ✓ D.\t
- E.\z
Why A: \n is a standard escape sequence in Python that represents a newline character (ASCII LF, 0x0A). It is defined in the Python language specification and is commonly used to insert line breaks in string literals.
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.