The simplest way to write a python multiline string is to wrap the text in triple quotes, either """ or '''. Everything between the opening and closing triple quotes, including line breaks, becomes part of the string. You can also build multi-line text with \n escapes, implicit concatenation of adjacent literals inside parentheses, or "\n".join(lines).
This guide is written for beginners and works on Python 3.13 and 3.14.
Triple quotes keep the newlines exactly as typed:
repr() shows the real content: each line break is a \n character. Single and double quotes can appear freely inside a triple-quoted string, as long as you do not write three of the same quote in a row.
Triple-quoted text keeps all leading spaces, so indenting it inside a function adds unwanted spaces:
The second line begins with four spaces. Two common fixes:
textwrap.dedentThe backslash right after the opening quotes stops the string from starting with an empty first line. dedent removes the whitespace that all lines share.
Python joins string literals that sit next to each other. Inside parentheses, this lets you indent freely and decide where the line breaks go:
No + is needed, and there is no newline unless you write \n. Forgetting a comma in a list of such literals silently merges two items, so be careful in lists.
When lines come from data, join them:
To split a multi-line string back into lines, use splitlines(), which handles \n, \r\n and other line endings:
Add the f prefix to insert values:
Add r for raw text where backslashes stay literal, which helps with regular expressions in verbose mode:
Since Python 3.12, expressions inside an f-string's braces can span several lines and contain comments, which helps with longer expressions.
A triple-quoted string ends with a newline only if you leave one before the closing quotes. Compare the two forms:
When you print a string that already ends in a newline, print adds a second one, which shows up as a blank line. Pass end="" or strip the text first when that matters. Text read from a file with read() keeps line endings translated to \n in text mode.
A triple-quoted string that is not assigned to anything is just an expression that does nothing. When it is the first statement in a module, class or function it becomes the docstring. Do not use stray triple-quoted strings as block comments: use # lines instead, which editors can toggle with one shortcut.
Put a backslash after the opening triple quotes ("""\) or call .strip() on the result to remove surrounding whitespace.
''' and """?None for strings. Both produce the same multi-line string. PEP 257 recommends """ for docstrings.
Wrap adjacent literals in parentheses, with no \n, so Python joins them into one line: ("part one " "part two").
\n work the same on Windows?Yes. Python translates \n to the platform line ending when you write text files in text mode, so you do not write \r\n yourself.
Use triple quotes for text that naturally spans lines, textwrap.dedent to remove indentation inside functions, parentheses with adjacent literals to control exactly where newlines appear, and "\n".join() when lines come from a list. Add f or r prefixes when you need values or literal backslashes.