Courseiva
Strings →mediumMultiple Select

PCAP Strings Practice Question

Which THREE of the following are valid escape sequences in Python strings?

⚠ Common exam trap

Python Institute often tests the distinction between valid escape sequences and invalid ones that are silently treated as literal characters, leading candidates to mistakenly think any backslash-letter combination is valid.

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

✓

\t

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

Answer analysis

Option-by-option breakdown

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

  • ✗

    \g

    Why it's wrong here

    The sequence \g is invalid in Python because the backslash escape mechanism only recognizes a fixed set of characters (e.g., \n, \t, \r, \\, \', \") and 'g' is not among them. In a regular string literal, an unrecognized escape like \g is technically retained as a literal backslash followed by 'g' (with a DeprecationWarning in newer Python versions), and in a raw string it would just be two characters. Thus, \g does not represent any special control character and is not a valid escape sequence.

  • ✓

    \t

    Why this is correct

    The sequence \t is a valid escape sequence that represents the tab character (ASCII 9, horizontal tab). It is commonly used to insert whitespace alignment in text output, for example in printing columns or formatting tables. Python recognizes \t as a single character with the ordinal value 9, not as two separate characters backslash and 't'.

  • ✗

    \h

    Why it's wrong here

    The sequence \h is invalid because 'h' is not defined as an escape character in Python's lexical specification. Unlike \n or \t, there is no control character associated with \h, so it does not produce a special character. In practice, Python preserves the backslash in the string (with a possible DeprecationWarning), making \h just two literal characters rather than a single escape.

  • ✓

    \r

    Why this is correct

    The sequence \r is a valid escape sequence that represents the carriage return character (ASCII 13). It originated from typewriters and early terminals, where it moves the cursor to the beginning of the line without advancing to the next line. In Python, \r is often used in text processing to overwrite the current line in console output or to handle files with Windows-style line endings (where it appears before \n).

  • ✓

    \n

    Why this is correct

    The sequence \n is a valid escape sequence that represents the newline character (ASCII 10, line feed). It is the standard line terminator in Unix/Linux and macOS text files, and Python normalizes line endings to \n when reading text files in universal newline mode. In strings, \n is a single character that creates a line break when printed, and it is also used as the default separator in print() calls.

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 →

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.