Python strings and coding conventions

Discussion in 'Python' started by koranthala, Jan 11, 2009.

  1. koranthala

    koranthala Guest

    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:.... abcd
    .... efgh
    .... ''''\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 am sure this is a very usual issue and I am missing some very
    very simple solution. But I cannot think of it at all...
     
    koranthala, Jan 11, 2009
    #1
    1. Advertisements

  2. koranthala

    Robert Kern Guest

    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
     
    Robert Kern, Jan 11, 2009
    #2
    1. Advertisements

  3. koranthala

    Mensanator Guest

    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:
    'our fathers brought forth ', \
    'on this continent a new nation ', \
    'conceived in liberty and dedicated ', \
    'to the proposition that all men ', \
    'are created equal']'four score and seven years ago our fathers brought forth on this
    continent a new nation conceived in liberty and dedicated to the
    proposition that all men are created equal'
     
    Mensanator, Jan 11, 2009
    #3
  4. koranthala

    koranthala Guest

    This is a very good method.
    I found another method too - on further investigation'abcefg'
    Only problem being that it doesnt support indentation.
    So, implicit concatenation it is...
     
    koranthala, Jan 11, 2009
    #4
  5. What are all those line continuation characters ('\') for? You are aware
    that they are unnecessary here?

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Jan 11, 2009
    #5
  6. koranthala

    Steve Holden Guest

    Another option is escaping the newlines in triple-quoted strings:

    regards
    Steve
     
    Steve Holden, Jan 11, 2009
    #6
  7. koranthala

    MRAB Guest

    Another possibility is continuation _plus_ implicit concatenation.

    def example():
    message = "now is the time " \
    "for all good people ..."
    print message
     
    MRAB, Jan 11, 2009
    #7
  8. koranthala

    Mensanator Guest

    Actually, I wasn't aware of that. A quick review shows
    why. In the old manuals, implicit line continuation
    was in a seperate chapter (2.1.6) from implicit (2.1.5)
    so if you didn't read past 2.1.5 you would have missed it.

    The 2.6 manual is much better in this regard as it is
    now difficult to miss.

    Thanks for pointing that out as lists are just about
    the only place I use line continuation.
     
    Mensanator, Jan 11, 2009
    #8
  9. koranthala

    Roy Smith Guest

    My philosophy about line continuation is to assume lines can be continued
    after just about any piece of punctuation. If I'm wrong, the computer will
    tell me, and then I make the computer happy by adding a \. It's easier
    than looking it up, and way easier than memorizing the details.
     
    Roy Smith, Jan 11, 2009
    #9
  10. koranthala

    Mensanator Guest

    I proably got mine from Visual Basic where there are no
    exceptions to explicit line continuation marks. A least
    adding them when not necessary doesn't cause a problem.
     
    Mensanator, Jan 11, 2009
    #10
  11. koranthala

    John Machin Guest

    Fugly code is not a problem?
     
    John Machin, Jan 11, 2009
    #11
  12. koranthala

    Mensanator Guest

    Is that how you justify breaing "explicit is
    better than implicit"?
     
    Mensanator, Jan 11, 2009
    #12
  13. koranthala

    John Machin Guest

    I don't '''justify breaing "explicit is better than implicit"''' and
    wasn't attempting to do so.

    The Zen is more than one saying. No one saying trumps all others. Some
    may appear to be in conflict with others. One must consider the
    combined effect. Other sayings relevant to your problem are:

    Beautiful is better than ugly.
    Simple is better than complex.
    Readability counts.

    HTH,
    John
     
    John Machin, Jan 11, 2009
    #13
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.