[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Break lines?

saneman

2/26/2008 3:45:00 PM

I have made this string:


TITLE = 'Efficiency of set operations: sort model,
(cphstl::set::insert(p,e)^n cphstl::set::insert(e)), integer'

But I am not allowed to break the line like that:

IndentationError: unexpected indent

How do I break a line?
3 Answers

Tim Chase

2/26/2008 4:01:00 PM

0

> I have made this string:
>
> TITLE = 'Efficiency of set operations: sort model,
> (cphstl::set::insert(p,e)^n cphstl::set::insert(e)), integer'
>
> But I am not allowed to break the line like that:
>
> IndentationError: unexpected indent
>
> How do I break a line?

Depends on what you want. You can embed running strings with
newlines using triple-quotes (either single- or double-quotes):

TITLE = """Efficiency...
(cphstl:..."""


Or you can use string concatenation using line-continuations:

TITLE = "Efficiency..." "(cphstl:..."

or using parens

TITLE = ("Efficiency..."
"(cphstl:...")



I like the clean'ness of the first version, but sometimes get
irked by it including my leading whitespace (there are some
workarounds, but all involve more than trivial effort). I tend
to use the 2nd in the case you describe, but usually using the
3rd version in all other cases where it's as a parameter to a
function call or some other bracketed/braced construct.

-tkc


saneman

2/26/2008 4:06:00 PM

0

Tim Chase wrote:
>> I have made this string:
>>
>> TITLE = 'Efficiency of set operations: sort model,
>> (cphstl::set::insert(p,e)^n cphstl::set::insert(e)), integer'
>>
>> But I am not allowed to break the line like that:
>>
>> IndentationError: unexpected indent
>>
>> How do I break a line?
>
> Depends on what you want. You can embed running strings with newlines
> using triple-quotes (either single- or double-quotes):
>
> TITLE = """Efficiency...
> (cphstl:..."""
>
>
> Or you can use string concatenation using line-continuations:
>
> TITLE = "Efficiency..." > "(cphstl:..."
>
> or using parens
>
> TITLE = ("Efficiency..."
> "(cphstl:...")
>
>
>
> I like the clean'ness of the first version, but sometimes get irked by
> it including my leading whitespace (there are some workarounds, but all
> involve more than trivial effort). I tend to use the 2nd in the case
> you describe, but usually using the 3rd version in all other cases where
> it's as a parameter to a function call or some other bracketed/braced
> construct.
>
> -tkc
>
>

Ok thanks! Btw why double quotes " instead of single ' ?

Tim Chase

2/26/2008 4:45:00 PM

0

> Ok thanks! Btw why double quotes " instead of single ' ?

Either one will do...there's not much difference. I try to use
double-quotes most of the time, just so when I include an
apostrophe in-line (which I do more often than I include a
double-quote in-line), I don't have to think.

string1a = "John's dog"
string1b = 'John\'s dog'
string2a = "She said \"hello\""
string2b = 'She said "hello"'
string3a = 'She said "John\'s nice" in a letter'
string3b = "She said \"John's nice\" in a letter'
string3c = """She said "John's nice" in a letter"""
string3d = '''She said "John's nice" in a letter'''

My usual aim is for clarity, so I tend to go with the versions
that have the fewest backslashes in them.

-tkc