Courseiva
Strings →hardMultiple Select

PCAP Strings Practice Question

Which THREE are valid escape sequences in Python strings?

⚠ Common exam trap

Python Institute often tests the distinction between valid escape sequences (like \n, \\, \t) and invalid ones (like \q, \z) that beginners might assume exist because they see other backslash combinations in contexts like regex or shell scripting.

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

✓

\n

\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.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✓

    \n

    Why this is correct

    In Python string literals, the backslash introduces an escape sequence, and \n specifically represents the line feed character (ASCII 10), which advances the cursor to the next line. This is one of the most commonly used escapes for creating multiline output or separating lines in text. Although written as two characters in source code, \n is stored as a single character in the resulting string, making it a single indexable element.

  • ✗

    \q

    Why it's wrong here

    The sequence \q is not a recognized escape sequence in Python's lexical specification. Unlike \n or \t, Python does not map \q to any special character, so the backslash is retained literally in the string, resulting in the two-character substring '\q'. In Python 3.12 and later, such invalid escapes trigger a SyntaxWarning to alert developers that the sequence is likely a mistake, though the code still runs without a syntax error.

  • ✓

    \\

    Why this is correct

    The double backslash \\ is the escape sequence for a single literal backslash character (ASCII 92). Because a single backslash begins an escape sequence, a literal backslash must be escaped by writing two backslashes. This is especially crucial in Windows file paths, where a single backslash would otherwise be interpreted as the start of an escape, and in regular expression patterns, where backslashes are common and need to be preserved verbatim.

  • ✓

    \t

    Why this is correct

    The \t escape sequence represents the horizontal tab character (ASCII 9), which advances the cursor to the next tab stop rather than inserting a fixed number of spaces. It is widely used for aligning columns of text in console output or for creating consistent indentation in generated files. Like all standard escape sequences, \t is written as two characters in source code but occupies only one character position in the string object.

  • ✗

    \z

    Why it's wrong here

    The sequence \z is not a defined escape sequence in Python, just as \q is not. It does not have any special meaning, so the resulting string contains a backslash immediately followed by the letter 'z' as two distinct characters. This invalid escape produces a SyntaxWarning in Python 3.12 and later, indicating that the backslash is probably unintended. Unlike \n or \t, which affect formatting, \z has no effect on string content beyond adding a backslash.

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 →

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.