Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #5080
| From | Raymond Hettinger <python@rcn.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: string formatting |
| Date | 2011-05-10 10:20 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <a4ef3f19-c832-40d8-81af-f10ceb4b184e@35g2000prp.googlegroups.com> (permalink) |
| References | <mailman.1229.1304666321.9059.python-list@python.org> |
> 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?
Sometimes, I use both ;-)
That can save you from ugly escapes such as %%s or {{0}}.
Here's an example from the standard library:
http://hg.python.org/cpython/file/7254c03b7180/Lib/collections.py#l235
Note the template has both {typename} formatting for the first pass
and %r style formatting in the generated code.
Raymond
------------
follow my tips and recipes on twitter: @raymondh
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar
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