472,144 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,144 software developers and data experts.

Triple-quoted strings hath not the Python-nature

If triple-quoted strings had the Python-nature, then they would take
indentation into account. Thus:

"""this
is a
multi-line
string."""

would be equivalent to

"this\n is a\n multi-line\nstring."

and not

"this\n is a\n multi-line\n string."

The rule would be: the exact same whitespace characters at the beginning of
the line on which the triple-quoted string starts must also occur at the
start of the lines on which the string continues; these are stripped off
and not included in the string contents. Any additional whitespace is of
course part of the string.
Oct 21 '08 #1
8 4145
from textwrap import dedent

dedent("""\
this
is a
multi-line
string.""")

will do what you want

On Tue, Oct 21, 2008 at 9:58 AM, Lawrence D'Oliveiro
<ld*@geek-central.gen.new_zealandwrote:
If triple-quoted strings had the Python-nature, then they would take
indentation into account. Thus:

"""this
is a
multi-line
string."""

would be equivalent to

"this\n is a\n multi-line\nstring."

and not

"this\n is a\n multi-line\n string."

The rule would be: the exact same whitespace characters at the beginning of
the line on which the triple-quoted string starts must also occur at the
start of the lines on which the string continues; these are stripped off
and not included in the string contents. Any additional whitespace is of
course part of the string.
--
http://mail.python.org/mailman/listinfo/python-list


--
or*****@orestis.gr
http://orestis.gr
Oct 21 '08 #2
On Tue, 21 Oct 2008 21:58:57 +1300, Lawrence D'Oliveiro wrote:
If triple-quoted strings had the Python-nature, then they would take
indentation into account. Thus:

"""this
is a
multi-line
string."""

would be equivalent to

"this\n is a\n multi-line\nstring."

and not

"this\n is a\n multi-line\n string."

The rule would be: the exact same whitespace characters at the beginning
of the line on which the triple-quoted string starts must also occur at
the start of the lines on which the string continues; these are stripped
off and not included in the string contents. Any additional whitespace
is of course part of the string.
"Although practicality beats purity." -- The Zen of Python, by Tim Peters

I would feel greatly offended if I had to indent all *raw* data.

--
Robert "Stargaming" Lehmann
Oct 21 '08 #3
That should be "Triple-quoted strings HAVE not the Python-nature."
'Hath' is the archaic 3rd person SINGULAR form of 'to have,' as in "a
tripple-quoted string hath ..."
Oct 22 '08 #4
On Tue, 21 Oct 2008 21:58:57 +1300, Lawrence D'Oliveiro wrote:
If triple-quoted strings had the Python-nature, then they would take
indentation into account. Thus:

"""this
is a
multi-line
string."""

would be equivalent to

"this\n is a\n multi-line\nstring."

and not

"this\n is a\n multi-line\n string."
I disagree. Triple-quoted strings are exactly the same as other strings:
they capture *exactly* what you put in them, and don't add or subtract
characters which the language designer imagines might be irrelevant for
some people some of the time.

" xyz " gives the exact string " xyz " and not "xyz", no matter how
convenient such behaviour would be for those who want only "xyz". If you
want to strip whitespace from the string, you can strip whitespace from
the string yourself.

Similarly

"""abc
xyz """

results in the exact string you put inside the quotes. Python doesn't try
to guess whether or not the spaces are significant. If you want to strip
whitespace, or any other character, you can do so yourself.

In other words, triple-quoted strings absolutely DO have the Python-
nature, because they refuse to guess what the programmer intends to do
with the string later. If you don't want the spaces, either don't put
them in in the first place, or remove them yourself. Don't expect Python
to guess whether you want the spaces or not.

--
Steven
Oct 23 '08 #5
In message <01**********************@news.astraweb.com>, Steven D'Aprano
wrote:
I disagree. Triple-quoted strings are exactly the same as other strings:
they capture *exactly* what you put in them ...
But that conflicts with the use of whitespace for indentation rules. Other
languages are freeform, and have strings that include whitespace as
significant.

In short, if whitespace is significant outside a string, then it shouldn't
be significant inside. And vice versa.
Oct 26 '08 #6
In message <48***********************@news.freenet.de>, Robert Lehmann
wrote:
I would feel greatly offended if I had to indent all *raw* data.
You mean raw strings?
Oct 26 '08 #7
Mel
Lawrence D'Oliveiro wrote:
In message <01**********************@news.astraweb.com>, Steven D'Aprano
wrote:
>I disagree. Triple-quoted strings are exactly the same as other strings:
they capture *exactly* what you put in them ...

But that conflicts with the use of whitespace for indentation rules. Other
languages are freeform, and have strings that include whitespace as
significant.

In short, if whitespace is significant outside a string, then it shouldn't
be significant inside. And vice versa.
I think the rule is that whitespace leading a statement is significant.
Whitespace inside a statement isn't. For example, whitespace inside a pair
of parentheses isn't significant:

Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>if True:
.... print (
.... 'a b d c d')
....
a b d c d
>>>

Oct 27 '08 #8
On Mon, 27 Oct 2008 12:11:34 +1300, Lawrence D'Oliveiro wrote:
In message <01**********************@news.astraweb.com>, Steven D'Aprano
wrote:
>I disagree. Triple-quoted strings are exactly the same as other
strings: they capture *exactly* what you put in them ...

But that conflicts with the use of whitespace for indentation rules.
No it doesn't. Indentation is a token in Python source code. Strings are
a data type. Syntax rules don't apply to strings because strings are
data, not syntax.

You wouldn't expect the following string literal to raise a syntax error:

"if Time = Money then ..."

Nor does the string "C++", and the string "1/0" doesn't raise
ZeroDivisionError. Strings are data, not syntax. Except for the syntactic
sugar of escape sequences, the contents of strings have no syntax.

(I say content to distinguish it from the delimiters themselves.)

Why should whitespace in string literals be treated as syntactic tokens
when no other characters are?
[...]
In short, if whitespace is significant outside a string, then it
shouldn't be significant inside. And vice versa.
That's an arbitrary rule that makes no sense at all. It's not just
arbitrary, it's *stupid*. Why on earth should the string literal

s = """
text
aligned
to
the
right
"""

raise an IndentationError? Or should it be a SyntaxError?
--
Steven
Oct 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Christoph Zwerschke | last post: by
reply views Thread by JB | last post: by
5 posts views Thread by Candace | last post: by
gchq
2 posts views Thread by gchq | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.