Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How do you write a multiline string in Python?

Python
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 27, 2026
5 min read

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.

Table of Contents

  • Triple-quoted strings
  • The indentation problem
    • Remove the indentation with textwrap.dedent
    • Use adjacent string literals
  • Building multi-line text from a list
  • Combining multiline strings with f-strings and raw strings
  • Line endings and trailing newlines
  • Multi-line strings versus comments and docstrings
  • Frequently Asked Questions
    • How do I avoid the extra newline at the start?
    • What is the difference between ''' and """?
    • How do I write a long single-line string across several lines?
    • Does \n work the same on Windows?
  • Summary

Triple-quoted strings

Triple quotes keep the newlines exactly as typed:

python
text

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.

The indentation problem

Triple-quoted text keeps all leading spaces, so indenting it inside a function adds unwanted spaces:

python

The second line begins with four spaces. Two common fixes:

Remove the indentation with textwrap.dedent

python

The backslash right after the opening quotes stops the string from starting with an empty first line. dedent removes the whitespace that all lines share.

Use adjacent string literals

Python joins string literals that sit next to each other. Inside parentheses, this lets you indent freely and decide where the line breaks go:

python

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.

Building multi-line text from a list

When lines come from data, join them:

python
text

To split a multi-line string back into lines, use splitlines(), which handles \n, \r\n and other line endings:

python

Combining multiline strings with f-strings and raw strings

Add the f prefix to insert values:

python

Add r for raw text where backslashes stay literal, which helps with regular expressions in verbose mode:

python

Since Python 3.12, expressions inside an f-string's braces can span several lines and contain comments, which helps with longer expressions.

Line endings and trailing newlines

A triple-quoted string ends with a newline only if you leave one before the closing quotes. Compare the two forms:

python

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.

Multi-line strings versus comments and docstrings

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.

Frequently Asked Questions

How do I avoid the extra newline at the start?

Put a backslash after the opening triple quotes ("""\) or call .strip() on the result to remove surrounding whitespace.

What is the difference between ''' and """?

None for strings. Both produce the same multi-line string. PEP 257 recommends """ for docstrings.

How do I write a long single-line string across several lines?

Wrap adjacent literals in parentheses, with no \n, so Python joins them into one line: ("part one " "part two").

Does \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.

Summary

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.

Share

Similar posts

  • How do you format numbers with f-strings in Python?
  • How do you use f-strings in Python?
  • What are escape sequences in Python strings?
  • Why are strings immutable in Python?
  • How do you check if a string is a number in Python?
  • How do you count occurrences of a substring in Python?
  • How do you check if a string contains a substring in Python?
  • How do you change the case of a string in Python?