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?
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.
Spanning strings over multiple lines can be done using python's triple quotes. It can also be used for long comments in code.
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 ”').
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).
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.
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}
\"\"\""""
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 '
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
"""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With