Python Reference ยท String Methods
Python string literals and escapes
Reference for Python string literals: single, double and triple quotes, escape sequences, raw strings, bytes literals, Unicode escapes and implicit concatenation.
A string literal is text written directly in your code. Python lets you choose between single quotes, double quotes and triple quotes, add a prefix to change how the contents are read (r, b, f, u), and use backslash escapes to write characters that are hard to type, such as a newline or a quote inside a quote.
Knowing these rules prevents most "why does my path look wrong" and "why is this regex broken" problems.
print('It\'s "quoted"\tand tabbed')
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Quotes
'text'and"text"are identical. Pick one style for the project and switch only to avoid backslashes.'''text'''and"""text"""may span lines and contain unescaped single quotes and double quotes. The newlines and indentation inside become part of the string.- A backslash followed by a real newline inside a normal literal continues the string on the next line without adding a newline.
- Adjacent literals are joined at compile time:
"a" "b"is"ab". Wrap multi-line text in parentheses to use this safely.
Escape sequences
| Escape | Meaning |
|---|---|
\\ |
Backslash |
\' and \" |
Single and double quote |
\n, \r, \t |
Newline, carriage return, tab |
\b, \f, \v, \a |
Backspace, form feed, vertical tab, bell |
\0 and \ooo |
Octal character code (up to three digits) |
\xhh |
Character with two hex digits |
\uhhhh |
Unicode code point, four hex digits |
\Uhhhhhhhh |
Unicode code point, eight hex digits |
\N{name} |
Unicode character by name |
An unknown escape such as "\d" is kept as written, but since Python 3.12 it raises a SyntaxWarning (a DeprecationWarning before that). Use a raw string or double the backslash.
Prefixes
r(raw): backslashes are kept literally, which suits regular expressions and Windows paths. A raw string cannot end with an odd number of backslashes, and\"still keeps the quote in the string (along with the backslash).b(bytes): produces abytesobject. Only ASCII characters may appear directly; use\xescapes for other values.\uand\Nare not escapes in bytes.f(formatted): evaluates; see string formatting. Combine asrf"...".u: accepted for backward compatibility and means nothing in Python 3.t(template string, 3.14): like an f-string but produces astring.templatelib.Templateobject instead of astr, for libraries that process the parts safely.
Usage notes
repr()versusprint().reprshows escapes ('a\nb');printshows the real characters. Usereprwhen debugging whitespace.- Length counts characters, not bytes:
len("รฉ")is 1, whilelen("รฉ".encode())is 2. - Encoding. Encode with
.encode("utf-8")and decode with.decode("utf-8"); mixing the two types raisesTypeError. - Windows paths like
"C:\new"silently contain a newline. Preferr"C:\new", forward slashes, orpathlib.
Related
- string formatting,
str.encode(),repr(),ascii(),bytes,str - Lessons: Strings, Regular expressions