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


Groups > comp.lang.python > #4803

Re: string formatting

Newsgroups comp.lang.python
Followup-To comp.lang.python
From Web Dreamer <webdreamer@nospam.fr>
Subject Re: string formatting
Date 2011-05-06 10:00 +0200
References <mailman.1229.1304666321.9059.python-list@python.org>
Message-ID <4dc3aaa0$0$4756$426a74cc@news.free.fr> (permalink)
Organization Guest of ProXad - France

Followups directed to: comp.lang.python

Show all headers | View raw


Jabba Laci a écrit ce vendredi 6 mai 2011 09:18 dans 
<mailman.1229.1304666321.9059.python-list@python.org> :

> Hi,
> 
> Which is the preferred way of string formatting?
> 
> (1) "the %s is %s" % ('sky', 'blue')
> 
> (2) "the {0} is {1}".format('sky', 'blue')
> 
> (3) "the {} is {}".format('sky', 'blue')
> 
> As I know (1) is old style. (2) and (3) are new but (3) is only
> supported from Python 2.7+.
> 
> Which one should be used?

According to python's documentation, (3) and (2) are preferred and (1) is 
deprecated.
HOWEVER, it depends on the target on which your code is going to run.
i.e. there are plenty of Web servers running python 2.5 (RedHat 5 or 4 
servers i.e., even MacOS OSX 10.5 servers) for which you may be asked to 
develop an application.
So if you develop, let's say a django or pylons web application for a 
customer who already has his own servers, you have more chances to get it 
running on most servers with (1), only few servers will work with (2) but a 
lot won't with (3) (very few 'enterprise server distros' ship with python 
2.7 "yet"). You can't always "impose" a distribution or python version to 
someone who already has his own servers in production.

If you develop a distro specific application, then start using (2) or (3) if 
the python version supports it.
If it's for a portable web application using a framework, use (1) to 
guarantee it will work with older servers which are running a still 
supported distribution. And you will then start using (2) when all server 
OSes shipped with python 2.5 will have reached end of support, and start 
using (3) only when all server OSes shipped with python 2.6 will have 
reached end of support.

So it really depends on the target it's supposed to run, and depends also 
whether you will have control or not on the target OS and python version.

-- 
Web Dreamer

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


Thread

string formatting Jabba Laci <jabba.laci@gmail.com> - 2011-05-06 03:18 -0400
  Re: string formatting Web Dreamer <webdreamer@nospam.fr> - 2011-05-06 10:00 +0200
    Re: string formatting Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-06 08:46 +0000
      Re: string formatting Chris Rebert <clp2@rebertia.com> - 2011-05-06 02:23 -0700
        Re: string formatting Web Dreamer <webdreamer@nospam.fr> - 2011-05-06 14:10 +0200
          Re: string formatting nn <pruebauno@latinmail.com> - 2011-05-06 06:06 -0700
          Re: string formatting Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-06 14:33 +0000
          Re: string formatting Web Dreamer <webdreamer@nospam.fr> - 2011-05-10 16:15 +0200
            Re: string formatting Chris Angelico <rosuav@gmail.com> - 2011-05-11 00:43 +1000
        Re: string formatting Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-06 14:24 +0000
          Re: string formatting harrismh777 <harrismh777@charter.net> - 2011-05-06 14:22 -0500
            Re: string formatting harrismh777 <harrismh777@charter.net> - 2011-05-06 14:39 -0500
              Re: string formatting Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-06 13:54 -0600
              Re: string formatting Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-07 02:30 +0000
              Re: string formatting alex23 <wuwei23@gmail.com> - 2011-05-08 22:47 -0700
            Re: string formatting Neil Cerutti <neilc@norwich.edu> - 2011-05-06 19:54 +0000
            Re: string formatting Terry Reedy <tjreedy@udel.edu> - 2011-05-06 16:54 -0400
            Re: string formatting Chris Angelico <rosuav@gmail.com> - 2011-05-07 10:09 +1000
            Re: string formatting Terry Reedy <tjreedy@udel.edu> - 2011-05-07 03:55 -0400
  Re: string formatting Raymond Hettinger <python@rcn.com> - 2011-05-10 10:20 -0700

csiph-web