## 2.4.1 String and Bytes literals 

String literals are described by the following lexical definitions: 

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> 

bytesliteral ::= bytesprefix ( shortbytes | longbytes ) bytesprefix ::= shortbytes ::= "'" shortbytesitem * "'" | '"' shortbytesitem * '"' longbytes ::= "'''" longbytesitem * "'''" | '"""' longbytesitem * '"""' shortbytesitem ::= shortbyteschar | bytesescapeseq longbytesitem ::= longbyteschar | bytesescapeseq shortbyteschar ::= <any ASCII character except "" or newline or the quote> longbyteschar ::= <any ASCII character except ""> bytesescapeseq ::= "" <any ASCII character> 

One syntactic restriction not indicated by these productions is that whitespace is not allowed between the $_{stringprefix}$or$_{bytesprefix}$ and the rest of the literal. The source character set is defined by the encoding declaration; it is UTF-8 if no encoding declaration is given in the source file; see section Encoding declarations . 

In plain English: Both types of literals can be enclosed in matching single quotes ($_{'}$ ) or double quotes ($_{"}$ ). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings ). The backslash ($_{\}$) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. 

Bytes literals are always prefixed with$_{'b'}$ or$_{'B'}$ ; they produce an instance of the$_{bytes}$type instead of the$_{str}$ type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes. 

Both string and bytes literals may optionally be prefixed with a letter$_{'r'}$ or$_{'R'}$ ; such strings are called raw strings and treat backslashes as literal characters. As a result, in string literals,$_{'\U'}$ and$_{'\u'}$ escapes in raw strings are not treated specially. Given that Python 2.x’s raw unicode literals behave differently than Python 3.x’s the$_{'ur'}$ syntax is not supported. 

New in version 3.3: The$_{'rb'}$ prefix of raw bytes literals has been added as a synonym of$_{'br'}$ . 

New in version 3.3: Support for the unicode legacy literal ($_{u'value'}$ ) was reintroduced to simplify the maintenance of dual Python 2.x and 3.x codebases. See PEP 414 for more information. 

A string literal with$_{'f'}$ or$_{'F'}$ in its prefix is a formatted string literal ; see Formatted string literals . The$_{'f'}$ may be combined with$_{'r'}$ , but not with$_{'b'}$ or$_{'u'}$ , therefore raw formatted strings are possible, but formatted bytes literals are not. 

In triple-quoted literals, unescaped newlines and quotes are allowed (and are retained), except that three unescaped quotes in a row terminate the literal. (A “quote” is the character used to open the literal, i.e. either$_{'}$or$_{"}$ .) 

Unless an$_{'r'}$ or$_{'R'}$ prefix is present, escape sequences in string and bytes literals are interpreted according to rules similar to those used by Standard C. The recognized escape sequences are: 

