Python String Literals and creation
Python String Literals and creation
Literals and creation
String literals are written as a series of characters in quotes, optionally preceded with a designator character, and in all string literal forms an empty string is coded as adjacent quotes. Various built-in operations also return new strings:
'Python“s', “Python's”
Single and double quotes work the same, and each can embed unescaped quotes of the other kind.
”““This is a multiline block””“
Triple-quoted blocks collect multiple lines of text into a single string, with end-of-line markers (\n) inserted between the original quoted lines.
'Python\'s\n'
Backslash escape code sequences (see Table 7) are replaced with the special-character code point values they represent (e.g., '\n' is an ASCII character with decimal code-point value 10).
“This” “is” “concatenated”
Adjacent string constants are concatenated. Hint: this form may span lines if parenthesized.
r'a raw\string', R'another\one'
Raw strings: backslashes are retained literally (except at the end of a string). Useful for regular expressions and Windows (DOS) directory paths: e.g., r'c:\dir1\file'.
hex(), oct(), bin()
Create hex/octal/binary digit strings from integer numbers. See Numbers and Built-in Functions.
The following literal forms and calls make specialized strings described in Unicode Strings:
b'…'
bytes string literal in Python 3.X: sequence of 8-bit byte values representing raw binary data. For 3.X compatibility, this form is also available in Python 2.6 and 2.7, where it simply creates a normal str string. See String methods, Unicode Strings, and Built-in Functions.
bytearray(…)
bytearray string construction: a mutable variant of bytes. Available in Python 3.X, and in Python 2.X as of 2.6. See String methods, Unicode Strings, and Built-in Functions.
u'…'
Unicode string literal in Python 2.X: a sequence of Unicode code points. For 2.X compatibility, this form is also available in Python 3.X as of 3.3, where it simply creates a normal str string (but normal string literals and str strings support Unicode text in Python 3.X). See Unicode Strings.
str(), bytes(), bytearray() (and unicode() in 2.X only)
Create strings from objects, with possible Unicode encoding/decoding in Python 3.X. See Built-in Functions.
String literals may contain escape sequences taken from Table 7 to represent special characters.
Table 7. String constant escape codes
Escape
Meaning
Escape
Meaning
\newline
Ignored continuation
\t
Horizontal tab
Backslash (\)
\v
Vertical tab
\'
Single quote (‘)
\N{id}
Unicode dbase id
\”
Double quote (“)
\uhhhh
Unicode 16-bit hex
\a
Bell
\Uhhhhhhhh
Unicode 32-bit hex[a]
\b
Backspace
\xhh
Hex (at most 2 digits)
\f
Formfeed
\ooo
Octal (up to 3 digits)
\n
Line feed
\0
Null (not end of string)
\r
Carriage return
\other
Not an escape
[a] \Uhhhhhhhh takes exactly eight hexadecimal digits (h); both \u and \U can be used only in Unicode string literals.