assigning multi-line strings to variables

Discussion in 'Python' started by goldtech, Apr 28, 2010.

  1. goldtech

    goldtech Guest

    Hi,

    This is undoubtedly a newbie question. How doI assign variables
    multiline strings? If I try this i get what's cited below. Thanks.


    >>> d="ddddd

    ddddd"
    >>> d

    Traceback (most recent call last):
    File "<interactive input>", line 1, in <module>
    NameError: name 'd' is not defined
     
    goldtech, Apr 28, 2010
    #1
    1. Advertising

  2. goldtech

    Brendan Abel Guest

    On Apr 27, 7:20 pm, goldtech <> wrote:
    > Hi,
    >
    > This is undoubtedly a newbie question. How doI assign variables
    > multiline strings? If I try this i get what's cited below. Thanks.
    >
    > >>> d="ddddd

    > ddddd"
    > >>> d

    >
    > Traceback (most recent call last):
    >   File "<interactive input>", line 1, in <module>
    > NameError: name 'd' is not defined




    d = "ddddddddd"\
    "ddddd"

    or

    d = "dddddddddd\
    dddddd"

    You don't need the trailing slash in the first example if you are
    writing this in a script, python assumes it.
     
    Brendan Abel, Apr 28, 2010
    #2
    1. Advertising

  3. goldtech

    MRAB Guest

    goldtech wrote:
    > Hi,
    >
    > This is undoubtedly a newbie question. How doI assign variables
    > multiline strings? If I try this i get what's cited below. Thanks.
    >
    >
    >>>> d="ddddd

    > ddddd"
    >>>> d

    > Traceback (most recent call last):
    > File "<interactive input>", line 1, in <module>
    > NameError: name 'd' is not defined


    Use a triple-quoted string literal:

    >>> d = """ddddd

    .... ddddd"""
    >>> d

    'ddddd\nddddd'
     
    MRAB, Apr 28, 2010
    #3
  4. goldtech

    goldtech Guest

    On Apr 27, 7:31 pm, Brendan Abel <> wrote:
    > On Apr 27, 7:20 pm, goldtech <> wrote:
    >
    > > Hi,

    >
    > > This is undoubtedly a newbie question. How doI assign variables
    > > multiline strings? If I try this i get what's cited below. Thanks.

    >
    > > >>> d="ddddd

    > > ddddd"
    > > >>> d

    >
    > > Traceback (most recent call last):
    > >   File "<interactive input>", line 1, in <module>
    > > NameError: name 'd' is not defined

    >
    > d = "ddddddddd"\
    >     "ddddd"
    >
    > or
    >
    > d = "dddddddddd\
    > dddddd"
    >
    > You don't need the trailing slash in the first example if you are
    > writing this in a script, python assumes it.


    Thanks but what if the string is 500 lines. Seems it would be hard to
    put a "\" manually at the end of every line. How could i do that?
     
    goldtech, Apr 28, 2010
    #4
  5. goldtech

    goldtech Guest

    On Apr 27, 7:33 pm, MRAB <> wrote:
    > goldtech wrote:
    > > Hi,

    >
    > > This is undoubtedly a newbie question. How doI assign variables
    > > multiline strings? If I try this i get what's cited below. Thanks.

    >
    > >>>> d="ddddd

    > > ddddd"
    > >>>> d

    > > Traceback (most recent call last):
    > >   File "<interactive input>", line 1, in <module>
    > > NameError: name 'd' is not defined

    >
    > Use a triple-quoted string literal:
    >
    >  >>> d = """ddddd
    > ... ddddd"""
    >  >>> d
    > 'ddddd\nddddd'


    Only seems to work when there's a '... ' on the 2nd line. I need a way
    to assign large blocks of text to a variable w/out special formatting.
    Thanks.
     
    goldtech, Apr 28, 2010
    #5
  6. goldtech

    MRAB Guest

    goldtech wrote:
    > On Apr 27, 7:33 pm, MRAB <> wrote:
    >> goldtech wrote:
    >>> Hi,
    >>> This is undoubtedly a newbie question. How doI assign variables
    >>> multiline strings? If I try this i get what's cited below. Thanks.
    >>>>>> d="ddddd
    >>> ddddd"
    >>>>>> d
    >>> Traceback (most recent call last):
    >>> File "<interactive input>", line 1, in <module>
    >>> NameError: name 'd' is not defined

    >> Use a triple-quoted string literal:
    >>
    >> >>> d = """ddddd

    >> ... ddddd"""
    >> >>> d

    >> 'ddddd\nddddd'

    >
    > Only seems to work when there's a '... ' on the 2nd line. I need a way
    > to assign large blocks of text to a variable w/out special formatting.
    > Thanks.


    I copied it from an interactive session in IDLE. In an actual script I
    would write:

    text = """first line
    second line
    third line"""
     
    MRAB, Apr 28, 2010
    #6
  7. goldtech

    Sagar K Guest

    Use triple quote:
    d = """ this is
    a sample text
    which does
    not mean
    anything"""

    "goldtech" <> wrote in message
    news:...
    On Apr 27, 7:31 pm, Brendan Abel <> wrote:
    > On Apr 27, 7:20 pm, goldtech <> wrote:
    >
    > > Hi,

    >
    > > This is undoubtedly a newbie question. How doI assign variables
    > > multiline strings? If I try this i get what's cited below. Thanks.

    >
    > > >>> d="ddddd

    > > ddddd"
    > > >>> d

    >
    > > Traceback (most recent call last):
    > > File "<interactive input>", line 1, in <module>
    > > NameError: name 'd' is not defined

    >
    > d = "ddddddddd"\
    > "ddddd"
    >
    > or
    >
    > d = "dddddddddd\
    > dddddd"
    >
    > You don't need the trailing slash in the first example if you are
    > writing this in a script, python assumes it.


    Thanks but what if the string is 500 lines. Seems it would be hard to
    put a "\" manually at the end of every line. How could i do that?
     
    Sagar K, Apr 28, 2010
    #7
  8. On 28.04.2010 07:11, * Sagar K:
    > Use triple quote:
    > d = """ this is
    > a sample text
    > which does
    > not mean
    > anything"""
    >
    > "goldtech"<> wrote in message
    > news:...
    > On Apr 27, 7:31 pm, Brendan Abel<> wrote:
    >> On Apr 27, 7:20 pm, goldtech<> wrote:
    >>
    >>> Hi,

    >>
    >>> This is undoubtedly a newbie question. How doI assign variables
    >>> multiline strings? If I try this i get what's cited below. Thanks.

    >>
    >>>>>> d="ddddd
    >>> ddddd"
    >>>>>> d

    >>
    >>> Traceback (most recent call last):
    >>> File "<interactive input>", line 1, in<module>
    >>> NameError: name 'd' is not defined

    >>
    >> d = "ddddddddd"\
    >> "ddddd"
    >>
    >> or
    >>
    >> d = "dddddddddd\
    >> dddddd"
    >>
    >> You don't need the trailing slash in the first example if you are
    >> writing this in a script, python assumes it.

    >
    > Thanks but what if the string is 500 lines. Seems it would be hard to
    > put a "\" manually at the end of every line. How could i do that?


    That depends. You can put the string in a separate text file and read the file,
    or you can have it as a literal. For the latter your editor should provide you
    with the tools to format the string any which way you want, and if not, then
    just a write a Python script to format it for you.

    Consider this little example[1]:


    <code file="jabberwocky.py">
    "The Jabberwocky poem, by Lewis Carrol"

    text = (
    "'Twas brillig, and the slithy toves\n"
    "Did gyre and gimble in the wabe;\n"
    " All mimsy were the borogoves,\n"
    " And the mome raths outgrabe.\n"
    "\n"
    "\"Beware the jabberwock, my son!\n"
    " The jaws that bite, the claws that catch!\n"
    "Beware the jubjub bird, and shun\n"
    " The frumious bandersnatch!\"\n"
    "\n"
    "He took his vorpal sword in hand:\n"
    " Long time the manxome foe he sought--\n"
    "So rested he by the tumtum tree,\n"
    " And stood awhile in thought.\n"
    "\n"
    "And as in uffish thought he stood,\n"
    " The jabberwock, with eyes of flame,\n"
    "Came whiffling through the tulgey wood,\n"
    " And burbled as it came!\n"
    "\n"
    "One, two! one, two! and through and through\n"
    " The vorpal blade went snicker-snack!\n"
    "He left it dead, and with its head\n"
    " He went galumphing back.\n"
    "\n"
    "\"And hast thou slain the jabberwock?\n"
    " Come to my arms, my beamish boy!\n"
    "O frabjous day! callooh! callay!\"\n"
    " He chortled in his joy.\n"
    "\n"
    "'Twas brillig, and the slithy toves\n"
    "Did gyre and gimble in the wabe;\n"
    " All mimsy were the borogoves,\n"
    " And the mome raths outgrabe."
    )
    </code>


    This defines /one/ string value, using compile time concatenation (any adjacent
    string literals are concatenated at compile time, in Python[2] and in C++).

    The text was just copied and pasted from Wikipedia, and subjected to a few well
    chosen keystrokes in an editor.

    As a hopefully illuminating exercise, consider a Python program that uses this
    string (just import the above module and use its 'text') and generates the above
    source code as output.


    Cheers & hth.,

    - Alf

    Notes:
    [1] From an example at the end of chapter 2 at <url:
    http://tinyurl.com/programmingbookP3>.
    [2] I'm not sure how well that plays with Python doc strings; haven't tried.
     
    Alf P. Steinbach, Apr 28, 2010
    #8
  9. goldtech

    cjw Guest

    On 27-Apr-10 22:31 PM, Brendan Abel wrote:
    > On Apr 27, 7:20 pm, goldtech<> wrote:
    >> Hi,
    >>
    >> This is undoubtedly a newbie question. How doI assign variables
    >> multiline strings? If I try this i get what's cited below. Thanks.
    >>
    >>>>> d="ddddd

    >> ddddd"
    >>>>> d

    >>
    >> Traceback (most recent call last):
    >> File "<interactive input>", line 1, in<module>
    >> NameError: name 'd' is not defined

    >
    >
    >
    > d = "ddddddddd"\
    > "ddddd"
    >
    > or
    >
    > d = "dddddddddd\
    > dddddd"
    >
    > You don't need the trailing slash in the first example if you are
    > writing this in a script, python assumes it.

    What about:
    *** Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit
    (Intel)] on win32. ***
    >>> a= ''' Now is the time for

    .... all good men
    .... to come to the aid
    .... of the party'''
    >>> print (a)

    Now is the time for
    all good men
    to come to the aid
    of the party
    >>>


    Colin W.
     
    cjw, Apr 28, 2010
    #9
  10. goldtech

    Lie Ryan Guest

    On 04/28/10 15:34, Alf P. Steinbach wrote:
    > On 28.04.2010 07:11, * Sagar K:
    >> Use triple quote:
    >> d = """ this is
    >> a sample text
    >> which does
    >> not mean
    >> anything"""
    >>
    >> "goldtech"<> wrote in message
    >> news:...
    >> On Apr 27, 7:31 pm, Brendan Abel<> wrote:
    >>> On Apr 27, 7:20 pm, goldtech<> wrote:
    >>>
    >>>> Hi,
    >>>
    >>>> This is undoubtedly a newbie question. How doI assign variables
    >>>> multiline strings? If I try this i get what's cited below. Thanks.
    >>>
    >>>>>>> d="ddddd
    >>>> ddddd"
    >>>>>>> d
    >>>
    >>>> Traceback (most recent call last):
    >>>> File "<interactive input>", line 1, in<module>
    >>>> NameError: name 'd' is not defined
    >>>
    >>> d = "ddddddddd"\
    >>> "ddddd"
    >>>
    >>> or
    >>>
    >>> d = "dddddddddd\
    >>> dddddd"
    >>>
    >>> You don't need the trailing slash in the first example if you are
    >>> writing this in a script, python assumes it.

    >>
    >> Thanks but what if the string is 500 lines. Seems it would be hard to
    >> put a "\" manually at the end of every line. How could i do that?

    >
    > That depends. You can put the string in a separate text file and read
    > the file, or you can have it as a literal. For the latter your editor
    > should provide you with the tools to format the string any which way you
    > want, and if not, then just a write a Python script to format it for you.
    >
    > Consider this little example[1]:

    <snip>

    Python have triple-quoted string when you want to include large amount
    of text; there is no need to split the string up manually or even
    scriptically.

    d = """
    ’Twas brillig, and the slithy toves
    Did gyre and gimble in the wabe;
    All mimsy were the borogoves,
    And the mome raths outgrabe.

    “Beware the Jabberwock, my son!
    The jaws that bite, the claws that catch!
    Beware the Jubjub bird, and shun
    The frumious Bandersnatch!”

    He took his vorpal sword in hand:
    Long time the manxome foe he sought—
    So rested he by the Tumtum tree,
    And stood awhile in thought.

    And as in uffish thought he stood,
    The Jabberwock, with eyes of flame,
    Came whiffling through the tulgey wood,
    And burbled as it came!

    One, two! One, two! and through and through
    The vorpal blade went snicker-snack!
    He left it dead, and with its head
    He went galumphing back.

    “And hast thou slain the Jabberwock?
    Come to my arms, my beamish boy!
    O frabjous day! Callooh! Callay!”
    He chortled in his joy.

    ’Twas brillig, and the slithy toves
    Did gyre and gimble in the wabe;
    All mimsy were the borogoves,
    And the mome raths outgrabe.
    """

    I copied that in less then 10 seconds.
     
    Lie Ryan, Apr 28, 2010
    #10
  11. On 28.04.2010 18:54, * Lie Ryan:
    > On 04/28/10 15:34, Alf P. Steinbach wrote:
    >> On 28.04.2010 07:11, * Sagar K:
    >>> Use triple quote:
    >>> d = """ this is
    >>> a sample text
    >>> which does
    >>> not mean
    >>> anything"""
    >>>
    >>> "goldtech"<> wrote in message
    >>> news:...
    >>> On Apr 27, 7:31 pm, Brendan Abel<> wrote:
    >>>> On Apr 27, 7:20 pm, goldtech<> wrote:
    >>>>
    >>>>> Hi,
    >>>>
    >>>>> This is undoubtedly a newbie question. How doI assign variables
    >>>>> multiline strings? If I try this i get what's cited below. Thanks.
    >>>>
    >>>>>>>> d="ddddd
    >>>>> ddddd"
    >>>>>>>> d
    >>>>
    >>>>> Traceback (most recent call last):
    >>>>> File "<interactive input>", line 1, in<module>
    >>>>> NameError: name 'd' is not defined
    >>>>
    >>>> d = "ddddddddd"\
    >>>> "ddddd"
    >>>>
    >>>> or
    >>>>
    >>>> d = "dddddddddd\
    >>>> dddddd"
    >>>>
    >>>> You don't need the trailing slash in the first example if you are
    >>>> writing this in a script, python assumes it.
    >>>
    >>> Thanks but what if the string is 500 lines. Seems it would be hard to
    >>> put a "\" manually at the end of every line. How could i do that?

    >>
    >> That depends. You can put the string in a separate text file and read
    >> the file, or you can have it as a literal. For the latter your editor
    >> should provide you with the tools to format the string any which way you
    >> want, and if not, then just a write a Python script to format it for you.
    >>
    >> Consider this little example[1]:

    > <snip>
    >
    > Python have triple-quoted string when you want to include large amount
    > of text;


    Yes, that's been mentioned umpteen times in this thread, including the *very
    first* quoted sentence above.

    It's IMHO sort of needless to repeat that after quoting it, and providing yet
    another example right after quoting an example.

    Probably you didn't notice?


    > there is no need to split the string up manually or even
    > scriptically.


    Consider that the concatenation language feature probably is there because it's
    useful (e.g. it preserves indentation and allows per line comments).


    > d = """
    > ’Twas brillig, and the slithy toves


    Here you have introduced an unintentional linebreak, oops.


    > Did gyre and gimble in the wabe;
    > All mimsy were the borogoves,
    > And the mome raths outgrabe.
    >
    > “Beware the Jabberwock, my son!
    > The jaws that bite, the claws that catch!
    > Beware the Jubjub bird, and shun
    > The frumious Bandersnatch!”
    >
    > He took his vorpal sword in hand:
    > Long time the manxome foe he sought—
    > So rested he by the Tumtum tree,
    > And stood awhile in thought.
    >
    > And as in uffish thought he stood,
    > The Jabberwock, with eyes of flame,
    > Came whiffling through the tulgey wood,
    > And burbled as it came!
    >
    > One, two! One, two! and through and through
    > The vorpal blade went snicker-snack!
    > He left it dead, and with its head
    > He went galumphing back.
    >
    > “And hast thou slain the Jabberwock?
    > Come to my arms, my beamish boy!
    > O frabjous day! Callooh! Callay!”
    > He chortled in his joy.
    >
    > ’Twas brillig, and the slithy toves
    > Did gyre and gimble in the wabe;
    > All mimsy were the borogoves,
    > And the mome raths outgrabe.
    > """
    >
    > I copied that in less then 10 seconds.


    Doesn't matter how fast it is when it's not correct (or, from another point of
    view, if it doesn't need to be done correctly then it can be arbitrarily fast).

    Of course you can fix it, but since you posted it with errors I think you were
    not aware.

    In the end there are drawbacks to any way of doing it, so it's to a large degree
    a matter of personal preference, as I see it. I just mentioned two additional
    ways not yet discussed in the thread. Exemplifying one of them.


    Cheers,

    - Alf
     
    Alf P. Steinbach, Apr 28, 2010
    #11
  12. goldtech

    Lie Ryan Guest

    On 04/29/10 04:16, Alf P. Steinbach wrote:
    > On 28.04.2010 18:54, * Lie Ryan:
    >> On 04/28/10 15:34, Alf P. Steinbach wrote:

    >
    > Yes, that's been mentioned umpteen times in this thread, including the
    > *very first* quoted sentence above.
    >
    > It's IMHO sort of needless to repeat that after quoting it, and
    > providing yet another example right after quoting an example.
    >
    > Probably you didn't notice?


    I do, my complaints is that you're making it needlessly complex.

    >> there is no need to split the string up manually or even
    >> scriptically.

    >
    > Consider that the concatenation language feature probably is there
    > because it's useful (e.g. it preserves indentation and allows per line
    > comments).


    No, the implicit concatenation is there because Python didn't always
    have triple quoted string. Nowadays it's an artifact and triple quoted
    string is much preferred. Long dump of text in source code is usually
    (or should be) globals constant anyway and you have no problem about
    indentation in globals constant. The only ubiquitous place where you
    need to put long stream of triple-quoted in non-global is as docstring;
    and docstring already handles the problem with indentation.

    >> I copied that in less then 10 seconds.

    >
    > Doesn't matter how fast it is when it's not correct (or, from another
    > point of view, if it doesn't need to be done correctly then it can be
    > arbitrarily fast).


    It's the nature of the text, who cares about extra line breaks or two.
    If this string has been for regex, I'd probably care.

    > Of course you can fix it, but since you posted it with errors I think
    > you were not aware.


    I used triple-quote a lot, and I'm very aware of that.

    > In the end there are drawbacks to any way of doing it, so it's to a
    > large degree a matter of personal preference, as I see it. I just
    > mentioned two additional ways not yet discussed in the thread.
    > Exemplifying one of them.


    When you have long stream of text and you paste it in source code, it's
    usually for one-off scripts that you're too lazy to create a proper file
    to read from. You never want to have to do some tedious preprocessing
    before you can insert it to the source.

    There's never a good reason to insert very long streams of text using
    implicit concatenation; not since docstring is here.
     
    Lie Ryan, Apr 28, 2010
    #12
  13. On Thu, 29 Apr 2010 06:17:42 +1000, Lie Ryan wrote:

    >> Consider that the concatenation language feature probably is there
    >> because it's useful (e.g. it preserves indentation and allows per line
    >> comments).

    >
    > No, the implicit concatenation is there because Python didn't always
    > have triple quoted string.


    Do you have a source for that?

    Both triple-quoted strings and implicit concatenation go back to at least
    Python 1.4:

    http://docs.python.org/release/1.4/tut/node71.html
    http://docs.python.org/release/1.4/tut/node70.html



    --
    Steven
     
    Steven D'Aprano, Apr 29, 2010
    #13
  14. goldtech

    MRAB Guest

    Steven D'Aprano wrote:
    > On Thu, 29 Apr 2010 06:17:42 +1000, Lie Ryan wrote:
    >
    >>> Consider that the concatenation language feature probably is there
    >>> because it's useful (e.g. it preserves indentation and allows per line
    >>> comments).

    >> No, the implicit concatenation is there because Python didn't always
    >> have triple quoted string.

    >
    > Do you have a source for that?
    >
    > Both triple-quoted strings and implicit concatenation go back to at least
    > Python 1.4:
    >
    > http://docs.python.org/release/1.4/tut/node71.html
    > http://docs.python.org/release/1.4/tut/node70.html
    >

    The page here:

    http://svn.python.org/projects/python/branches/py3k/Misc/HISTORY

    says release 1.0.2 (4 May 1994).
     
    MRAB, Apr 29, 2010
    #14
  15. On Thu, 29 Apr 2010 02:16:46 +0100, MRAB wrote:

    > Steven D'Aprano wrote:
    >> On Thu, 29 Apr 2010 06:17:42 +1000, Lie Ryan wrote:
    >>
    >>>> Consider that the concatenation language feature probably is there
    >>>> because it's useful (e.g. it preserves indentation and allows per
    >>>> line comments).
    >>> No, the implicit concatenation is there because Python didn't always
    >>> have triple quoted string.

    >>
    >> Do you have a source for that?
    >>
    >> Both triple-quoted strings and implicit concatenation go back to at
    >> least Python 1.4:
    >>
    >> http://docs.python.org/release/1.4/tut/node71.html
    >> http://docs.python.org/release/1.4/tut/node70.html
    >>

    > The page here:
    >
    > http://svn.python.org/projects/python/branches/py3k/Misc/HISTORY
    >
    > says release 1.0.2 (4 May 1994).


    Yes, it says:

    * String literals follow Standard C rules: they may be continued
    on the next line using a backslash; adjacent literals are
    concatenated at compile time.

    * A new kind of string literals, surrounded by triple quotes
    (""" or '''), can be continued on the next line without a
    backslash.


    These are adjacent entries in the same release. That's pretty good
    evidence that both implicit concatenation and triple quotes were
    introduced at the same time.



    --
    Steven
     
    Steven D'Aprano, Apr 29, 2010
    #15
  16. Lie Ryan wrote:
    > No, the implicit concatenation is there because Python didn't always
    > have triple quoted string. Nowadays it's an artifact and triple quoted
    > string is much preferred.


    I don't agree. I often use implicit concatenation when I'm
    writing a format string that won't fit on one source line,
    because it allows me to fit it into the surrounding indentation
    structure without introducing unwanted spaces into the string.

    Both tecnhiques have their places.

    --
    Greg
     
    Gregory Ewing, Apr 29, 2010
    #16
  17. goldtech

    goldtech Guest

    Thank you to posters for help to my question. Seems I had trouble with
    triple quotes strings in the PythonWin shell. But using the Idle shell
    things work as expected. But this is probably another issue...any way,
    w/Idle's shell I got the "action" regarding multiline strings I
    expected.
     
    goldtech, Apr 29, 2010
    #17
  18. goldtech

    Lie Ryan Guest

    On 04/29/10 20:40, Gregory Ewing wrote:
    > Lie Ryan wrote:
    >> No, the implicit concatenation is there because Python didn't always
    >> have triple quoted string. Nowadays it's an artifact and triple quoted
    >> string is much preferred.

    >
    > I don't agree. I often use implicit concatenation when I'm
    > writing a format string that won't fit on one source line,
    > because it allows me to fit it into the surrounding indentation
    > structure without introducing unwanted spaces into the string.
    >
    > Both tecnhiques have their places.
    >


    That statement should be quantified with "for large chunks of text".
    Format string is typically 2-3 lines at most, not enough to qualify as
    large chunk.
     
    Lie Ryan, Apr 29, 2010
    #18
  19. goldtech

    Lie Ryan Guest

    On 04/29/10 16:34, Steven D'Aprano wrote:
    > On Thu, 29 Apr 2010 02:16:46 +0100, MRAB wrote:
    >
    >> Steven D'Aprano wrote:
    >>> On Thu, 29 Apr 2010 06:17:42 +1000, Lie Ryan wrote:
    >>>
    >>>>> Consider that the concatenation language feature probably is there
    >>>>> because it's useful (e.g. it preserves indentation and allows per
    >>>>> line comments).
    >>>> No, the implicit concatenation is there because Python didn't always
    >>>> have triple quoted string.
    >>>
    >>> Do you have a source for that?
    >>>
    >>> Both triple-quoted strings and implicit concatenation go back to at
    >>> least Python 1.4:
    >>>
    >>> http://docs.python.org/release/1.4/tut/node71.html
    >>> http://docs.python.org/release/1.4/tut/node70.html
    >>>

    >> The page here:
    >>
    >> http://svn.python.org/projects/python/branches/py3k/Misc/HISTORY
    >>
    >> says release 1.0.2 (4 May 1994).

    >
    > Yes, it says:
    >
    > * String literals follow Standard C rules: they may be continued
    > on the next line using a backslash; adjacent literals are
    > concatenated at compile time.
    >
    > * A new kind of string literals, surrounded by triple quotes
    > (""" or '''), can be continued on the next line without a
    > backslash.
    >
    >
    > These are adjacent entries in the same release. That's pretty good
    > evidence that both implicit concatenation and triple quotes were
    > introduced at the same time.


    Yes, apparently my statement that implicit concatenation is an artifact
    is erroneous but it doesn't make the important points less true, that
    implicit concatenation is not suitable for integrating large chunk of
    text into source code.

    And if you do care about the two extra newlines, you can add two
    backslashes:

    s = """\
    ....
    insert large chunks
    ....\
    """

    which is a constant-time preformatting compared to prepending and
    appending every line with quotes, which is O(n) (and we're talking about
    the oh-so-expensive programmer's time here).

    Ben Finney also showed the trick to let a large chunk to appear indented
    and stripping the indenting at runtime in another thread (which is
    actually rarely needed since, since as I previously said, huge chunk of
    text is usually global-level variable; though the trick does come handy
    sometimes).
     
    Lie Ryan, Apr 29, 2010
    #19
  20. goldtech

    Carl Banks Guest

    On Apr 28, 11:16 am, "Alf P. Steinbach" <> wrote:
    > On 28.04.2010 18:54, * Lie Ryan:


    > > Python have triple-quoted string when you want to include large amount
    > > of text;

    >
    > Yes, that's been mentioned umpteen times in this thread, including the *very
    > first* quoted sentence above.
    >
    > It's IMHO sort of needless to repeat that after quoting it, and providing yet
    > another example right after quoting an example.
    >
    > Probably you didn't notice?



    I think he repeated it just to let people know that they can get what
    they want without following your adsurd advice.


    Carl Banks
     
    Carl Banks, Apr 30, 2010
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    799
    Malcolm
    Jun 24, 2006
  2. Replies:
    6
    Views:
    352
    Steve Holden
    Mar 25, 2007
  3. Jiri Barton

    unicode in multi-line strings

    Jiri Barton, Sep 18, 2008, in forum: Python
    Replies:
    1
    Views:
    348
    Diez B. Roggisch
    Sep 18, 2008
  4. Catherine Lynn Wood

    Assigning large multi-line HTML/text blocks to variables

    Catherine Lynn Wood, Dec 3, 2003, in forum: Javascript
    Replies:
    0
    Views:
    162
    Catherine Lynn Wood
    Dec 3, 2003
  5. weston
    Replies:
    1
    Views:
    271
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page