Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a multi line string with triple quotes inside using Python?

I couldn't find this online but basically I have a string like this:

s = "name={0},
address={1},
nickname={2},
age={3},
comments=
"""
{4}
"""
"

and I need to format this string as is using variables like so:

s.format("alice", "N/A", "alice", 18, "missing person")

I can't change the triple quotes there because the program that will use my string expects that, otherwise won't work.

How can I have this string declared/escaped correctly?

like image 658
Joan Venge Avatar asked Jun 17 '16 02:06

Joan Venge


People also ask

How do you do triple quotes in Python?

Python's triple quotes comes to the rescue by allowing strings to span multiple lines, including verbatim NEWLINEs, TABs, and any other special characters. The syntax for triple quotes consists of three consecutive single or double quotes.

Can you write multi-line comments in Python using triple quotes?

Spanning strings over multiple lines can be done using python's triple quotes. It can also be used for long comments in code.

How do you quote multiple lines in Python?

if writing a text in a single line then use double quotes or single quotes in python. if writing a poem or songs or multi-line text then to use triple quotes(“”” or ”').

Can strings be enclosed in triple quotes?

String literals inside triple quotes, """ or ''', can span multiple lines of text. Python strings are "immutable" which means they cannot be changed after they are created (Java strings also use this immutable style).


2 Answers

You can escape triple-quotes inside a string the same way you can escape any other quote character, with a \:

s = """name={0},
address={1},
nickname={2},
age={3},
comments=
\"\"\"
{4}
\"\"\"
"""

Strictly speaking, you only have to escape one of the " characters --- enough to prevent a triple """ from appearing --- but I find escaping all three makes my intent more clear.

Later...

sf = s.format("alice", "N/A", "alice", 18, "missing person")
print(sf)
print('----')
print(repr(sf))

...produces:

name=alice,
address=N/A,
nickname=alice,
age=18,
comments=
"""
missing person
"""

----
'name=alice,\naddress=N/A,\nnickname=alice,\nage=18,\ncomments=\n"""\nmissing person\n"""\n'

niemmi's answer works, but only if you don't have a mix of ''' and """ triple-quotes inside the string. Escaping the quote characters with a backslash always works.

Annoyance #1: Trailing Newline

I printed the row of dashes to highlight that s has preserved the newline between the last three escaped quote characters and the triple quotes that actually ended the string. To remove it from the literal:

s = """[as before...]
\"\"\"
{4}
\"\"\""""

Annoyance #2: Indentations Preserved Inside Literal

The second and following lines of the s literal must be flush against the first (left-hand) column. Triple-quoted strings neatly lined up inside an indented block:

def indents_appear_in_string_literal():
    # This looks good but doesn't work right.
    s = """name={0},
    address={1},
    nickname={2},
    age={3},
    comments=
    \"\"\"
    {4}
    \"\"\"
    """
    sf = s.format("alice", "N/A", "alice", 18, "missing person")
    print(sf)
    print('----')
    print(repr(sf))
    return

...will preserve the indentations inside the literal:

name=alice,
    address=N/A,
    nickname=alice,
    age=18,
    comments=
    """
    missing person
    """

----
'name=alice,\n    address=N/A,\n    nickname=alice,\n    age=18,\n    comments=\n    """\n    missing person\n    """\n    '
like image 114
Kevin J. Chase Avatar answered Oct 03 '22 19:10

Kevin J. Chase


You could use triple single quotes for the string:

s = '''name={0},
address={1},
nickname={2},
age={3},
comments=
"""
{4}
"""
'''

print s.format("alice", "N/A", "alice", 18, "missing person")

Output:

name=alice,
address=N/A,
nickname=alice,
age=18,
comments=
"""
missing person
"""
like image 30
niemmi Avatar answered Oct 03 '22 21:10

niemmi