Skip to content
Tutorials Center
My learning

Topics

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.

Python
print('It\'s "quoted"\tand tabbed')

Try it

Python Quotes and common escapes

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.

Python Raw strings and bytes

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 a bytes object. Only ASCII characters may appear directly; use \x escapes for other values. \u and \N are not escapes in bytes.
  • f (formatted): evaluates ; see string formatting. Combine as rf"...".
  • u: accepted for backward compatibility and means nothing in Python 3.
  • t (template string, 3.14): like an f-string but produces a string.templatelib.Template object instead of a str, for libraries that process the parts safely.

Usage notes

  • repr() versus print(). repr shows escapes ('a\nb'); print shows the real characters. Use repr when debugging whitespace.
  • Length counts characters, not bytes: len("รฉ") is 1, while len("รฉ".encode()) is 2.
  • Encoding. Encode with .encode("utf-8") and decode with .decode("utf-8"); mixing the two types raises TypeError.
  • Windows paths like "C:\new" silently contain a newline. Prefer r"C:\new", forward slashes, or pathlib.