By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,023 Members | 1,235 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,023 IT Pros & Developers. It's quick & easy.

multiline snippets with triple quotes

P: n/a
I sometimes use triple quotes in order to produce snippets of multiline
code, like that:

if output == html:
snip = '''<html>
<head><title>Hello, World</title></head>
<body bgcolor="aqua"><h1>What's up?</h1>
</html>'''
else:
snip = 'Hello!'

This has the advantage that you don't need to care for inserting newlines
and escaping quotes and is very readable.

However, the triple quoted strings will also contain the indentation of the
Python block. It would not really matter here, since whitespace is ignored
in this case, but there could be situations where whitespace at the
beginning of the lines would matter, and it simply makes no sense to carry
over the identation in most cases.

Of course, I could simply unindent the triple quoted snippets, but then the
Python code starts to become unreadable, if you have many of such snippets
in deeply indented blocks. And isn't bad indentation something that Python
usually forbids "by force" even?

So, what would be the pythonic way to implement such multiline snippets?

Gtx
Chris
Jul 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Christoph Zwerschke wrote:
So, what would be the pythonic way to implement such multiline snippets?


Define them externally to that code block. Either put
them above the function, or at the top of the module,
or have them read from an external file, with the
best approach being dependent on the precise situation
at hand. For example, if you have only one or two
such things, just use parentheses and single quotes
instead of the triple quoting, while if you have a
bunch of them but don't want to read them from a file,
put them in a separate module and just import and
reference them as required.

-Peter
Jul 18 '05 #2

P: n/a
Christoph Zwerschke wrote:
I sometimes use triple quotes in order to produce snippets of multiline
code, like that:

if output == html:
snip = '''<html>
<head><title>Hello, World</title></head>
<body bgcolor="aqua"><h1>What's up?</h1>
</html>'''
else:
snip = 'Hello!'


[...]
So, what would be the pythonic way to implement such multiline snippets?


IIRC, sequential strings with only whitespace in between them are
automatically concatenated. So the above could be equivalently written as:

if output == html:
snip = "<html>\n"
"<head><title>Hello, World</title></head>\n"
"<body bgcolor="aqua"><h1>What's up?</h1>\n"
"</html>\n"
else:
snip = 'Hello!'
This does have the disadvantage of requiring explicit newlines, however.

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #3

P: n/a
On Thu, 12 Aug 2004, Christoph Zwerschke wrote:
So, what would be the pythonic way to implement such multiline snippets?


The textwrap.dedent() function is provided for just this purpose:

from textwrap import dedent

if output == html:
snip = dedent('''\
<html>
<head><title>Hello, World</title></head>
<body bgcolor="aqua"><h1>What's up?</h1>
</html>''')
else:
snip = 'Hello!'

textwrap.dedent() removes uniform indentation from the beginning of each
line in the string, so you can still insert desired indents in a natural
way. Note the backslash after the opening quote, and how the actual
string starts on the next line: dedent() isn't as smart as pydoc when it
comes to uniformly indenting strings; it doesn't know to skip the first
line (this helps with more general usage, though).

Jul 18 '05 #4

P: n/a

On Aug 12, 2004, at 4:06 PM, Christopher T King wrote:
The textwrap.dedent() function is provided for just this purpose:

from textwrap import dedent

if output == html:
snip = dedent('''\
<html>
<head><title>Hello, World</title></head>
<body bgcolor="aqua"><h1>What's up?</h1>
</html>''')
else:
snip = 'Hello!'

Can dedent capture indents or tabs also...
I noticed that if I do the following:
snip = '''<html>blah blah
</html>'''
Tabs are included with snip.

Is there a heredocs (don't know why it's called that) equivalent in
Python?

Thanks in advance.

Jul 18 '05 #5

P: n/a
> IIRC, sequential strings with only whitespace in between them are
automatically concatenated.
Yes, but you have to add either brackets around everything or backslashes at
the line ends, otherwise the lines are not kept together.
This does have the disadvantage of requiring explicit newlines, however.


Yes, plus you need to escape the double quotes.

These three disadvantages are why I wanted to use triple quotes.

Chris
Jul 18 '05 #6

P: n/a
> The textwrap.dedent() function is provided for just this purpose:

Thanks. That was the thing I was looking for, though it's a big ugly, plus
it eats some unnecessary CPU cycles at run time. But I think one can live
with that.

Chris
Jul 18 '05 #7

P: n/a
On Thu, 12 Aug 2004, gohaku wrote:
Can dedent capture indents or tabs also...
I noticed that if I do the following:
snip = '''<html>blah blah
</html>'''
Tabs are included with snip.
dedent() will return that string as is, since the <html> tag is already in
the left-most position. If you do something like this, however:

snip = '''\
<html>blah blah
</html>'''

dedent() will flush the <html> tag left, and the </html> tag will be
indented by one tab (since it has a one-tab indent relative to <html>).
Is there a heredocs (don't know why it's called that) equivalent in
Python?


AFAIK, triple quotes do everything heredocs do (but with a cleaner
syntax!); the following produce identical output:

bash heredocs:

echo -n <<EOF
Some text.
Woo!
The end.
EOF

Python triple quotes:

print '''\
Some text.
Woo!
The end.'''

Jul 18 '05 #8

P: n/a
On Fri, Aug 13, 2004 at 09:22:38AM -0400, Christopher T King wrote:

AFAIK, triple quotes do everything heredocs do (but with a cleaner
syntax!); the following produce identical output:

bash heredocs:

echo -n <<EOF
Some text.
Woo!
The end.
EOF

Python triple quotes:

print '''\
Some text.
Woo!
The end.'''
what about
cat <<-EOF 1
2
3
EOF

1
2
3

--
John Lenton (jo**@grulic.org.ar) -- Random fortune:
<JHM> Somehow I have more respect for 14 year old Debian developers than
14 year old Certified Microsoft Serfs.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFBHemBgPqu395ykGsRAqfKAJ9T/EjsfspD97012cUT++X5kwl7jwCgssxt
UiroR8OI+dNGDF3J1Ap6F/c=
=QuNS
-----END PGP SIGNATURE-----

Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.