Python strings and coding conventions
MRAB
google at mrabarnett.plus.com
Sun Jan 11 11:10:53 EST 2009
koranthala at gmail.com wrote:
> On Jan 11, 9:26 am, 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')
>>
>> --
>> Robert Kern
>>
>> "I have come to believe that the whole world is an enigma, a harmless enigma
>> that is made terrible by our own mad attempt to interpret it as though it had
>> an underlying truth."
>> -- Umberto Eco
>
> This is a very good method.
> I found another method too - on further investigation
>>>> s = "abc\
> ... efg"
>>>> s
> 'abcefg'
> Only problem being that it doesnt support indentation.
> So, implicit concatenation it is...
>
Another possibility is continuation _plus_ implicit concatenation.
def example():
message = "now is the time " \
"for all good people ..."
print message
More information about the Python-list
mailing list