Python strings and coding conventions

Steve Holden steve at holdenweb.com
Sun Jan 11 16:06:46 CET 2009


Mensanator wrote:
> On Jan 10, 10:26�pm, Robert Kern <robert.k... at gmail.com> wrote:
>> koranth... at gmail.com wrote:
>>> Hi,
>>> � �Python Coding Convention (PEP 8) suggests :
>>> � Maximum Line Length
>>> � � Limit all lines to a maximum of 79 characters.
>>> � I have a string which is ~110 char long. It is a string which I am
>>> going to print in a text file as a single string.
>>> � i.e. in that text file, each line is taken as a different record -
>>> so it has to be in a single line.
>>> � Now, how can I write this code - while following PEP 8?
>>> � I tried blockstrings, but as shown in the example below:
>>>>>> s = r'''
>>> ... abcd
>>> ... efgh
>>> ... '''
>>>>>> s
>>> '\nabcd\nefgh\n'
>>> � �it has "\n" inserted - which will disallow printing it to a single
>>> line.
>>> � �I thought about other options like concatenating strings etc, but
>>> it seems very kludgy - esp since the whole string has a single meaning
>>> and cannot be easily split to many logically. Then I thought of
>>> creating a blockstring and then removing "\n", but it seemed
>>> kludgier...
>> I usually use implicit concatenation:
>>
>> s = ('some long text that '
>> � � � 'needs to be split')
> 
> Damn! I didn't know you could do that! And if I
> saw it in a program listing, such would never occur
> to me. I was going to suggest the stupid way:
> 
Another option is escaping the newlines in triple-quoted strings:

>>> s = """Some long text that \
... needs to be split"""
>>> s
'Some long text that needs to be split'
>>>

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list