473,405 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 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 4242
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Christoph Zwerschke | last post by:
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...
0
by: Aravind | last post by:
Hi folks. I have a form, frmHistory, which has 3 toggle buttons (1 of which is tglName, which I will be using to demonstrate my problem). The buttons are used to sort the form (explanations...
0
by: JB | last post by:
Does anybody have an example triple nested repeater they could show me? Or even lead me through? Thanx, JB
2
by: maitrepoy | last post by:
hello I have to create a small addin which works on Powerpoint, Word, Outlook, and Excel on Office 2000, XP, and 2003. This addin consists in adding 2 new Buttons in the "File" Menu of office....
1
by: Wayne | last post by:
Not sure if anyone else has noticed this, but it gets around one of the more annoying limitations of using themed controls. Normally when using themed controls a triple state check box looks the...
15
by: Andy Mabbett | last post by:
Google finds: Results 1 - 20 of about 34,000 linking to http://www.w3.org/WAI/WCAG1AAA-Conformance -- Andy Mabbett Say "NO!" to compulsory ID Cards: <http://www.no2id.net/> Free Our...
5
by: Candace | last post by:
I have to write a program to find all Pythagorean triples for a right triangle. I know that the squares of two sides of the triangle must equal the square of the third (longest) side. However, I...
3
by: Kyriakos Petrakos | last post by:
Hello to everyone, Recently, I came across a scenario which required some data encryption routines applied to general binary files. I decided to use the managed code provided by .NET that...
2
by: TeenuSusan | last post by:
Hii All, I want to do Triple DES Encryption using C language.Can anybody help me...
2
gchq
by: gchq | last post by:
Hi there There is no problem encypting and decrypting a credit card number, but whilst encrypting the expiry date seems to work it blows out on decryption with "Invalid length for a Base-64 char...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.