1.2.1.3 PCEP Practice Test Compendium – Python basic types and literals

Python basic types and literals: continued

10) String literals are sequences (including empty ones) of characters (digits, letters, punctuation marks, etc.). There are two kinds of string literal:

  • single-line, when the string itself begins and ends in the same line of code: these literals are enclosed in a pair of ' (apostrophe) or " (quote) marks.

  • multi-line, when the string may extend to more than one line of code: these literals are enclosed in a pair of trigraphs either """ or '''

  • strings enclosed inside apostrophes can contain quotes, and vice versa.

  • if you need to put an apostrophe inside an apostrophe-limited string, or a quote inside a quote-limited string, you must precede them with the \ (backslash) sign, which acts as an escape character (a character which changes the meaning of the character that follows it); some of the most used escape sequences are:

    • \\ – backslash

    • \' – apostrophe

    • \" – quote

    • \n – newline character

    • \r – carriage return character

    • \t – horizontal tab character

11) Here are some examples of correct string literals:

  • "Hello world"

  • 'Goodbye!'

  • '' (an empty string)

  • "Python's den"

  • 'Python\'s den'

  • """Two lines"""

12) Boolean literals denote the only two possible values used by the Boolean algebra – their only acceptable denotations are True and False.

13) The None literal denotes an empty value and can be used to indicate that a certain item contains no usable value.

Last updated