Sideway from Sideway
Draft for Information Only


Python String Literal
 Definition of Python String Literal
 Features of Python String
  Python String Notation
  Python String Catalogue
  Python String Prefix
  Python Source Characters
  Escape Sequences Characters
 Python Unicode Literial
 Source and Reference

Python String Literal

A string is a sequence of characters. In other words, a string is basically a bunch of words and/or symbols. Although byte literal is one type of string literal in Python, the literal value of a byte literal is always considered as byte sequences or byte arrays.

Definition of Python String Literal

A string literal is a sequence of characters from any source characters.
stringliteral::=[stringprefix](shortstring | longstring)
    stringprefix::="r" | "u" | "R" | "U" | "f" | "F" | "fr" | "Fr" | "fR" | "FR" | "rf" | "rF" | "Rf" | "RF"
    shortstring::="'" shortstringitem* "'" | '"' shortstringitem* '"'
    longstring::="'''" longstringitem* "'''" | '"""' longstringitem* '"""'
    shortstringitem::=shortstringchar | stringescapeseq
    longstringitem::=longstringchar | stringescapeseq
    shortstringchar::=<any source character except "\" or newline or the quote>
    longstringchar::=<any source character except "\">
    stringescapeseq::="\" <any source character>

Features of Python String

The Python string of string literal in Python have some typical features.

Python String Notation

A Python string is denoted by quotes. The ways to denote a Python string are. Single Quotes, '...'e.g. 'A single quoted string' Double Quotes, "..."e.g. 'A double quoted string' Triple Quotes, '''...''' or """..."""e.g. '''A triple quoted string''', """A triple quoted string""" image

Python String Catalogue

Python strings can be divided into shortstring and longstring. The difference betweens shortstring and longstring are: shortstringEnclosed by single or double quotes. Does not allow reserved character backslash \, unescaped NEWLINE, and the corresponding quote ' or " image longstringEnclosed by triple quotes. Does not allow reserved character backslash \ only. Unescaped newlines and quotes are allowed (and are retained), except that three unescaped quotes in a row terminate the literal. image

Python String Prefix

Python String Prefix is used to specify the rules to interpret the following string literal. Whitespace is not allowed between the string prefix and the rest of the string literal. The meanings of supported string prefixs are: r, RA raw form string literal. The escape sequence function is turned off. Backslash \ is treated as an ordinary literal character. image u, UAn unicode string literal. This prefix is only used to restore support for the unicode legacy literal. image f, FA formatted string literal. A brace enclosed Python expression is enclosed in the string literal for formating. Using brace {or }. in formatted string literal outside f-string expression part should be replaced by double braces {{ or }}. Besides backslash \ cannot be used in f-string expression part. image fr, Fr, fR, FR, rf, rF, Rf, RFSince f-string expression part is evaluated separately, raw formatted string literals are possible. image Because backslashes \ in string literals with prefix r or R are treated as literal characters. And backslashes \ in string literals with \u and \U are always not treated as literal characters. Since the Python 2.x's raw unicode literals behave differently from that of the Python 3.x's, the ur syntax is not supported.

Python Source Characters

The Python source character set is defined by the encoding declaration. The Python source character set is UTF-8 if no encoding declaration is given in the source file.

Escape Sequences Characters

The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. The recognized escape sequences are: \<NEWLINE>Physically literal line break, logically BACKSLASH and NEWLINE are ignored. \\Backslash (\) \'Single quote (') \"Double quote (") \aASCII Bell (BEL) \bASCII Backspace (BS) \fASCII Formfeed (FF) \nASCII Linefeed (LF) \rASCII Carriage Return (CR) \tASCII Horizontal Tab (TAB) \vASCII Vertical Tab (VT) \oooCharacter with octal value ooo \xhhCharacter with hex value hh Escape sequences only recognized in string literals are: \N{name}Character named name in the Unicode database \uxxxxCharacter with 16-bit hex value xxxx \UxxxxxxxxCharacter with 32-bit hex value xxxxxxxx image

Python Unicode Literial

An unicode string literal is only used to restore support for the unicode legacy literal. The prefix u or U is used to denote an unicode string literal. Python reads program text as Unicode code points; the encoding of a source file can be given by an encoding declaration and defaults to UTF-8.

Source and Reference


ID: 210100002 Last Updated: 2/1/2021 Revision: 0


Home 5






Hobbies 8


Chinese 1097

English 337

Reference 67


Hardware 151


Application 202

Digitization 25

Latex 10

Manim 159

Numeric 19


Web 285

Unicode 495


CSS 59


OS 391

DeskTop 7

Python 46



Formulas 8

Algebra 29

Number Theory 206

Trigonometry 18

Geometry 18

Calculus 67

Complex Analysis 21


Tables 8


Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5


Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Electric 27

Biology 1

Geography 1

Copyright © 2000-2021 Sideway . All rights reserved Disclaimers last modified on 06 September 2019