Multiline Strings in Python

We covered how to create multiline strings in the Ruby programming language, but what about if you're using Python? Well in Python there are two ways to create a multiline string.

Triple Quotes for a Multiline Python String

You can use a triple quote or triple double-quote to create a multiline string in Python:

multiline = '''
a string
  with many lines
    in python.
'''

multiline = """
You can use double-quotes
  for a multiline string too,
  and "quotes" within the 'string'.
"""

Brackets to Enclose a Multiline String

The other way is to use a bracket to enclose multiple strings that use single or double quotes:

multiline = (
'first'
'second'
)

multiline = (
  "hello"
  "world"
)

You will need to add a '\n' at the end of each line to preserve the newlines. Just like this:

multiline = ("first line\n"
  "second line\n"
)