473,671 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4269
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************ ***********@new s.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
ZeroDivisionErr or. 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 IndentationErro r? 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
3225
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 bgcolor="aqua"><h1>What's up?</h1> </html>''' else: snip = 'Hello!'
0
2281
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 below). I have set the button's "Triple State" value to Yes, and the default value to False. Here is the macro that I used for tglName (I assigned the macro to the button's On Click event): ...
0
1119
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
414
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. This is properly done, but the events which should be triggered with the button.click method are not triggered in Word. I don't understand because it properly works in the 3 others host applications. If someone has an issue, I would be most...
1
4120
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 same in the unchecked and null states - very annoying and not all that useful. Today I found by accident that if the border style of the checkbox is set to anything other than "solid", the check box takes on the appearance of a non-themed...
15
2586
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 Data: <http://www.freeourdata.org.uk>
5
5229
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 am not sure how to use a triple-nested for...next loop to try all possibilities. For instance, take a look at these numbers: a 3 5 7 8 9 11 b 4 12 24 15 40 60 c 5 13 25 17 41 61
3
7324
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 implements the Triple Des algorithm (TripleDESCryptoServiceProvider object). Everything works fine as long as we stay within the .NET framework but what if the encryption or decryption routine is implemented on a different platform? (e.g. using Java's...
2
3705
by: TeenuSusan | last post by:
Hii All, I want to do Triple DES Encryption using C language.Can anybody help me...
2
2464
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 array" Any ideas? Here is the code - the keys are generated from a 40+ string held on another database (ReturnKey).... Private Function IV_192()
0
8909
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8596
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8667
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6222
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4221
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4399
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1801
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.