Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89551

Re: implicitly concats of adjacent strings does not work with format

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'subject:not': 0.03; 'syntax': 0.04; 'important,': 0.07; 'string': 0.09; 'method:': 0.09; "'to": 0.16; 'adjacent': 0.16; 'called.': 0.16; 'concatenate': 0.16; 'expression,': 0.16; 'literal,': 0.16; 'literal.': 0.16; 'literals': 0.16; 'subject:format': 0.16; 'syntaxerror:': 0.16; '{0}': 0.16; 'wrote:': 0.18; 'work,': 0.20; 'example': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'wondering': 0.29; 'am,': 0.29; "skip:' 10": 0.31; 'occurs': 0.31; 'third': 0.33; 'could': 0.34; 'subject:with': 0.35; 'something': 0.35; 'one,': 0.35; 'but': 0.35; 'method': 0.36; 'skip:. 20': 0.38; 'to:addr :python-list': 0.38; 'realize': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'called': 0.40; 'first': 0.61; "you've": 0.63; 'charset:windows-1252': 0.65; 'determine': 0.67; 'invalid': 0.68; 'received:74.208': 0.68; 'vital': 0.78; 'received:74.208.4.194': 0.84
Date Wed, 29 Apr 2015 09:14:59 -0400
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0
MIME-Version 1.0
To python-list@python.org
Subject Re: implicitly concats of adjacent strings does not work with format
References <877fsvcdhz.fsf@Equus.decebal.nl>
In-Reply-To <877fsvcdhz.fsf@Equus.decebal.nl>
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V03:K0:aFKfPBWy6DauCCkAxx9dZmFls8QAXDqWZjGOxoh3OMGYkOiKjJm bRWFKjxDlZX+C4dZrqDxhYLwmPncaxx2451ZVhXNyDZvPux9DuhwxC5ZzXidd98QiZM9C7G o/va49DAUP+FF9XQ97esbTweOE9SdhvjYy3992CMbSDHvEJvWw6hwAmduCAdZqQ2+MpK4EK qM5mpUNmOR47o9alhQoyg==
X-UI-Out-Filterresults notjunk:1;
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.86.1430313312.3680.python-list@python.org> (permalink)
Lines 69
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1430313312 news.xs4all.nl 2887 [2001:888:2000:d::a6]:39626
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:89551

Show key headers only | View raw


On 04/29/2015 08:42 AM, Cecil Westerhof wrote:
> I have the folowing print statements:
>      print(
>          'Calculating fibonacci_old, fibonacci_memoize and '
>          'fibonacci_memoize once for {0} '.format(large_fibonacci))
>
>
>      print(
>          'Calculating fibonacci_old, fibonacci_memoize and '
>          'fibonacci_memoize once for {0} '.format(large_fibonacci) +
>          'to determine speed increase')
>
>      print(
>          'Calculating fibonacci_old, fibonacci_memoize and '
>          'to determine speed increase'
>          'fibonacci_memoize once for {0} '.format(large_fibonacci))
>
>
>      print(
>          'Calculating fibonacci_old, fibonacci_memoize and '
>          'fibonacci_memoize once for {0} '.format(large_fibonacci)
>          'to determine speed increase')
>
> The first three work, but the last gives:
>          'to determine speed increase')
>                                      ^
>      SyntaxError: invalid syntax
>
> Not very important, because I can use the second one, but I was just
> wondering why it goes wrong.
>

Adjacent string literals are concatenated.  But once you've called a 
method (.format()) on that literal, you now have an expression, not a 
string literal.

You could either change the last line to

          + 'to determine speed increase')

or you could concatenate all the strings before calling the format method:


     print(
         'Calculating fibonacci_old, fibonacci_memoize and '
         'fibonacci_memoize once for {0} '
         'to determine speed increase' .format(large_fibonacci))

Something you may not realize is that the addjacent-concatenation occurs 
at compile time, so your third example could be transformed from:

     print(
         'Calculating fibonacci_old, fibonacci_memoize and '
         'to determine speed increase'
         'fibonacci_memoize once for {0} '.format(large_fibonacci))

to:
     print(
         'Calculating fibonacci_old, fibonacci_memoize and '
         'to determine speed increase'
         'fibonacci_memoize once for {0}'
         ' '.format(large_fibonacci))


All three literals are combined before format() is called.  Knowing this 
could be vital if you had {} elsewhere in the 9single) literal.

-- 
DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

implicitly concats of adjacent strings does not work with format Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 14:42 +0200
  Re: implicitly concats of adjacent strings does not work with format Dave Angel <davea@davea.name> - 2015-04-29 09:14 -0400
    Re: implicitly concats of adjacent strings does not work with format Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 16:40 +0200
      Re: implicitly concats of adjacent strings does not work with format Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-04-29 17:04 +0100
        Re: implicitly concats of adjacent strings does not work with format Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 19:08 +0200
          Re: implicitly concats of adjacent strings does not work with format Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-04-29 21:51 +0100
            Re: implicitly concats of adjacent strings does not work with format Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 23:51 +0200

csiph-web