[Tutor] Tutor Digest, Vol 53, Issue 13

Lie Ryan lie.1296 at gmail.com
Sun Jul 6 11:09:11 CEST 2008


> Message: 5
> Date: Fri, 04 Jul 2008 00:29:03 +0800
> From: Dong Li <dongli2020 at gmail.com>
> Subject: Re: [Tutor] Question about string
> To: tutor at python.org
> Message-ID: <1215102543.13683.4.camel at localhost.localdomain>
> Content-Type: text/plain
> 
> 
> > Date: Thu, 3 Jul 2008 10:18:23 +0100
> > From: "Alan Gauld" <alan.gauld at btinternet.com>
> > Subject: Re: [Tutor] Question about string
> > To: tutor at python.org
> > Message-ID: <g4i5h5$1cc$1 at ger.gmane.org>
> > Content-Type: text/plain; format=flowed; charset="iso-8859-1";
> >       reply-type=original
> > 
> > 
> > "Dong Li" <dongli2020 at gmail.com> wrote
> > 
> > > I am new to python, so what I ask may be so basic. I don't know
> the
> > > difference between
> > >
> > > s = 'a' 'b'
> > > and
> > > s = 'a'+'b'
> > >
> > > They have the same results. Thanks for relying!
> > 
> > I think the differencec is that the first is purely a syntax thing
> so
> > the interpreter does the work of joining the strings together before
> > processing the result as a single string whereas the second the
> > two strings are treated separately and actual string addition
> > (concatenation) is done which is a much more expensive
> > operation in terms of computer power.
> > 
> > The first is only possible if you have literal strings but the
> second
> > can be used for variables:
> > 
> > s1 = 'a'
> > s2 = 'b'
> > s = s1 s2     # doesn't work
> > s = s1 + s2   # works
> > 
> > HTH,
> > 
> > -- 
> > Alan Gauld
> > Author of the Learn to Program web site
> > http://www.freenetpages.co.uk/hp/alan.gauld 
> > 
> 
> > ------------------------------
> 
> > Date: Thu, 3 Jul 2008 09:53:07 +0000
> > From: "Monika Jisswel" <monjissvel at googlemail.com>
> > Subject: Re: [Tutor] Question about string
> > To: tutor at python.org
> > Message-ID:
> >       <e2f191310807030253p681550d0t76e69a4080055584 at mail.gmail.com>
> > Content-Type: text/plain; charset="iso-8859-1"
> > 
> > Python is one of the smartest languages, it does many things for the
> > programmer  (I don't know but this might be what they mean with
> > Batteries-Included) , & you have just scratched the surface of it,
> here
> > python concatenated your strings together for you, later you will
> meet list
> > comprehention & other stuff that actually does most of the
> programing logic
> > for you for free.
> > -------------- next part --------------
> > An HTML attachment was scrubbed...
> > URL:
> <http://mail.python.org/pipermail/tutor/attachments/20080703/9c54d12a/attachment-0001.htm>
> > 
> > ------------------------------
> 
> Thank you for excellent explanations! I have been attracted by python
> more and more!

The string implicit string concatenation exist for things like verbose
re (regular expression):

import re
re.compile(
'<'     # Start opening tag
'\s*'   # Arbitrary whitespace
'(.*?)' # tagname
'\s*'   # Arbitrary whitespace
'(.*?)' # Values
'>',    # Start closing tag
re.VERBOSE
)

Without the implicit string concatenation, that re would have to be
written like this:

import re
re.compile(
'<'     + \  # Start opening tag
'\s*'   + \  # Arbitrary whitespace
'(.*?)' + \  # tagname
'\s*'   + \  # Arbitrary whitespace
'(.*?)' + \  # Values
'>',    # Start closing tag
re.VERBOSE
)

or in one long lines, and comment is thus impossible.



More information about the Tutor mailing list