Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #18258 > unrolled thread
| Started by | davidfx <dgeorge29ca@gmail.com> |
|---|---|
| First post | 2011-12-31 10:19 -0800 |
| Last post | 2012-01-04 17:41 -0800 |
| Articles | 20 on this page of 42 — 20 participants |
Back to article view | Back to comp.lang.python
.format vs. % davidfx <dgeorge29ca@gmail.com> - 2011-12-31 10:19 -0800
Re: .format vs. % Andrew Berg <bahamutzero8825@gmail.com> - 2011-12-31 12:34 -0600
Re: .format vs. % Yaşar Arabacı <yasar11732@gmail.com> - 2011-12-31 20:34 +0200
Re: .format vs. % davidfx <dgeorge29ca@gmail.com> - 2011-12-31 10:44 -0800
Re: .format vs. % davidfx <dgeorge29ca@gmail.com> - 2011-12-31 10:44 -0800
Re: .format vs. % Yaşar Arabacı <yasar11732@gmail.com> - 2011-12-31 20:51 +0200
Re: .format vs. % Evan Driscoll <edriscoll@wisc.edu> - 2011-12-31 13:47 -0500
Re: .format vs. % Miki Tebeka <miki.tebeka@gmail.com> - 2012-01-01 13:11 -0800
Re: .format vs. % Terry Reedy <tjreedy@udel.edu> - 2012-01-02 05:29 -0500
Re: .format vs. % Miki Tebeka <miki.tebeka@gmail.com> - 2012-01-01 13:11 -0800
Re: .format vs. % Alexander Kapps <alex.kapps@web.de> - 2011-12-31 19:59 +0100
Re: .format vs. % Tim Chase <python.list@tim.thechases.com> - 2011-12-31 13:24 -0600
Re: .format vs. % Lie Ryan <lie.1296@gmail.com> - 2012-01-01 06:34 +1100
Re: .format vs. % Robert Kern <robert.kern@gmail.com> - 2011-12-31 19:43 +0000
Re: .format vs. % Terry Reedy <tjreedy@udel.edu> - 2011-12-31 16:09 -0500
Re: .format vs. % Ethan Furman <ethan@stoneleaf.us> - 2012-01-02 14:00 -0800
Re: .format vs. % Rick Johnson <rantingrickjohnson@gmail.com> - 2012-01-02 17:59 -0800
Re: .format vs. % Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-03 03:52 +0000
Re: .format vs. % Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-02 22:58 -0700
Re: .format vs. % Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-03 06:15 +0000
Re: .format vs. % Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-03 01:16 -0700
Re: .format vs. % Ethan Furman <ethan@stoneleaf.us> - 2012-01-03 05:26 -0800
Re: .format vs. % Ethan Furman <ethan@stoneleaf.us> - 2012-01-03 11:38 -0800
Re: .format vs. % Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-01-03 15:02 -0500
Re: .format vs. % Ethan Furman <ethan@stoneleaf.us> - 2012-01-03 12:16 -0800
Re: .format vs. % Andrew Berg <bahamutzero8825@gmail.com> - 2012-01-03 04:55 -0600
Re: .format vs. % Stefan Krah <stefan-usenet@bytereef.org> - 2012-01-03 12:47 +0100
Re: .format vs. % Neil Cerutti <neilc@norwich.edu> - 2012-01-03 13:58 +0000
Re: .format vs. % Stefan Krah <stefan-usenet@bytereef.org> - 2012-01-03 15:17 +0100
Re: .format vs. % Neil Cerutti <neilc@norwich.edu> - 2012-01-03 15:15 +0000
Re: .format vs. % Neil Cerutti <neilc@norwich.edu> - 2012-01-03 15:24 +0000
Re: .format vs. % Neil Cerutti <neilc@norwich.edu> - 2012-01-03 19:46 +0000
Re: .format vs. % Ethan Furman <ethan@stoneleaf.us> - 2012-01-03 12:00 -0800
Re: .format vs. % Stefan Krah <stefan-usenet@bytereef.org> - 2012-01-03 21:53 +0100
Re: .format vs. % Chris Angelico <rosuav@gmail.com> - 2012-01-04 00:04 +1100
Re: .format vs. % Rick Johnson <rantingrickjohnson@gmail.com> - 2012-01-02 17:51 -0800
Re: .format vs. % Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-03 03:47 +0000
Re: .format vs. % 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-03 07:47 -0800
Re: .format vs. % alex23 <wuwei23@gmail.com> - 2012-01-03 18:26 -0800
Re: .format vs. % 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-04 00:25 -0800
Re: .format vs. % alex23 <wuwei23@gmail.com> - 2012-01-04 16:23 -0800
Re: .format vs. % 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-04 17:41 -0800
Page 1 of 3 [1] 2 3 Next page →
| From | davidfx <dgeorge29ca@gmail.com> |
|---|---|
| Date | 2011-12-31 10:19 -0800 |
| Subject | .format vs. % |
| Message-ID | <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> |
Hello everyone,
I just have a quick question about .format and %r %s %d.
Should we always be using .format() for formatting strings or %?
Example a = 'apples'
print "I love {0}.".format(a)
If I wanted to put .format into a variable, how would I do that.
Thanks for your information in advance.
David
[toc] | [next] | [standalone]
| From | Andrew Berg <bahamutzero8825@gmail.com> |
|---|---|
| Date | 2011-12-31 12:34 -0600 |
| Message-ID | <mailman.4266.1325356467.27778.python-list@python.org> |
| In reply to | #18258 |
On 12/31/2011 12:19 PM, davidfx wrote: > Should we always be using .format() for formatting strings or %? In new code, yes. %-style formatting will eventually go away, but probably not for a long time. > If I wanted to put .format into a variable, how would I do that. What do you mean? -- CPython 3.2.2 | Windows NT 6.1.7601.17640
[toc] | [prev] | [next] | [standalone]
| From | Yaşar Arabacı <yasar11732@gmail.com> |
|---|---|
| Date | 2011-12-31 20:34 +0200 |
| Message-ID | <mailman.4267.1325356518.27778.python-list@python.org> |
| In reply to | #18258 |
What exactly do you mean by putting .format into a variable? You mean like
this:
"{name} is very {adj}
{gender}".format(name="sandy",adj="diligent",gender="female")
Sat, 31 Dec 2011 20:19:34 +0200 tarihinde davidfx <dgeorge29ca@gmail.com>
şöyle yazmış:
> Hello everyone,
> I just have a quick question about .format and %r %s %d.
>
> Should we always be using .format() for formatting strings or %?
>
> Example a = 'apples'
> print "I love {0}.".format(a)
>
> If I wanted to put .format into a variable, how would I do that.
>
> Thanks for your information in advance.
>
> David
--
Opera'nın e-posta istemcisi ile gönderildi: http://www.opera.com/mail
[toc] | [prev] | [next] | [standalone]
| From | davidfx <dgeorge29ca@gmail.com> |
|---|---|
| Date | 2011-12-31 10:44 -0800 |
| Message-ID | <mailman.4268.1325357057.27778.python-list@python.org> |
| In reply to | #18261 |
Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking. formatter = "%r %r %r %r" print formatter % (1, 2, 3, 4) What is the .format version of this concept?
[toc] | [prev] | [next] | [standalone]
| From | davidfx <dgeorge29ca@gmail.com> |
|---|---|
| Date | 2011-12-31 10:44 -0800 |
| Message-ID | <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> |
| In reply to | #18261 |
Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking. formatter = "%r %r %r %r" print formatter % (1, 2, 3, 4) What is the .format version of this concept?
[toc] | [prev] | [next] | [standalone]
| From | Yaşar Arabacı <yasar11732@gmail.com> |
|---|---|
| Date | 2011-12-31 20:51 +0200 |
| Message-ID | <mailman.4269.1325357588.27778.python-list@python.org> |
| In reply to | #18263 |
You mean like this?
===========================
>>> a = "I like {name}"
>>> a.format(name="myself")
'I like myself'
============================
Sat, 31 Dec 2011 20:44:08 +0200 tarihinde davidfx <dgeorge29ca@gmail.com>
şöyle yazmış:
> Thanks for your response. I know the following code is not going to be
> correct but I want to show you what I was thinking.
>
> formatter = "%r %r %r %r"
>
> print formatter % (1, 2, 3, 4)
>
> What is the .format version of this concept?
>
--
Opera'nın e-posta istemcisi ile gönderildi: http://www.opera.com/mail
[toc] | [prev] | [next] | [standalone]
| From | Evan Driscoll <edriscoll@wisc.edu> |
|---|---|
| Date | 2011-12-31 13:47 -0500 |
| Message-ID | <mailman.4270.1325357752.27778.python-list@python.org> |
| In reply to | #18263 |
[Multipart message — attachments visible in raw view] — view raw
How 'bout just:
>>> s = "{0} {1} {2} {3}"
>>> s.format(1, 2, 3, 4)
'1 2 3 4'
Evan
On 12/31/2011 13:44, davidfx wrote:
> Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking.
>
> formatter = "%r %r %r %r"
>
> print formatter % (1, 2, 3, 4)
>
> What is the .format version of this concept?
>
[toc] | [prev] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2012-01-01 13:11 -0800 |
| Message-ID | <2939615.568.1325452304773.JavaMail.geo-discussion-forums@vbdz6> |
| In reply to | #18265 |
> >>> s = "{0} {1} {2} {3}"
> >>> s.format(1, 2, 3, 4)
> '1 2 3 4'
Or even
In [4]: fmt = '{0} {1} {2} {3}'.format
In [5]: print(fmt(1, 2, 3, 4))
1 2 3 4
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-01-02 05:29 -0500 |
| Message-ID | <mailman.4304.1325500512.27778.python-list@python.org> |
| In reply to | #18296 |
On 1/1/2012 4:11 PM, Miki Tebeka wrote:
>>>>> s = "{0} {1} {2} {3}"
>>>>> s.format(1, 2, 3, 4)
>> '1 2 3 4'
> Or even
> In [4]: fmt = '{0} {1} {2} {3}'.format
> In [5]: print(fmt(1, 2, 3, 4))
> 1 2 3 4
I have done this, except for using a more informative name, like 'emsg'
for error message.
except XError as e:
print(emsg(a,b,c,e))
makes for pretty clear code.
--
Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2012-01-01 13:11 -0800 |
| Message-ID | <mailman.4294.1325452313.27778.python-list@python.org> |
| In reply to | #18265 |
> >>> s = "{0} {1} {2} {3}"
> >>> s.format(1, 2, 3, 4)
> '1 2 3 4'
Or even
In [4]: fmt = '{0} {1} {2} {3}'.format
In [5]: print(fmt(1, 2, 3, 4))
1 2 3 4
[toc] | [prev] | [next] | [standalone]
| From | Alexander Kapps <alex.kapps@web.de> |
|---|---|
| Date | 2011-12-31 19:59 +0100 |
| Message-ID | <mailman.4273.1325358726.27778.python-list@python.org> |
| In reply to | #18263 |
On 31.12.2011 19:44, davidfx wrote:
> Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking.
>
> formatter = "%r %r %r %r"
>
> print formatter % (1, 2, 3, 4)
>
> What is the .format version of this concept?
>
formatter = "{0} {1} {2} {3}"
formatter.format(1,2,3,4)
I have to say, I simply hate .format(), and I will be very, very
upset if classic formatting will really be removed.
I understand that .format() has a lot of advantages for more complex
formatting, but for the simple stuff I usually do, it's just
terribly inconvenient (try {} on a German keyboard).
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2011-12-31 13:24 -0600 |
| Message-ID | <mailman.4274.1325359459.27778.python-list@python.org> |
| In reply to | #18263 |
On 12/31/11 12:57, Benjamin Kaplan wrote:
> format is a method of the string class. You store the string the same way
> you would any other.
>
> formatter = "Hello, {}"
> print(formatter.format("world"))
Just to note that this syntax doesn't quite work in some earlier
versions (tested below in 2.6, which came stock in Debian Stable)
>>> formatter = "Hello, {}"
>>> print(formatter.format("world"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: zero length field name in format
It needs to be spelled "{0}"
-tkc
[toc] | [prev] | [next] | [standalone]
| From | Lie Ryan <lie.1296@gmail.com> |
|---|---|
| Date | 2012-01-01 06:34 +1100 |
| Message-ID | <mailman.4277.1325360101.27778.python-list@python.org> |
| In reply to | #18263 |
On 01/01/2012 05:44 AM, davidfx wrote: > Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking. > > formatter = "%r %r %r %r" > > print formatter % (1, 2, 3, 4) > > What is the .format version of this concept? > I don't think the (%r)epr-formatting exist anymore, so if you want to do that you'll need to call repr manually.
[toc] | [prev] | [next] | [standalone]
| From | Robert Kern <robert.kern@gmail.com> |
|---|---|
| Date | 2011-12-31 19:43 +0000 |
| Message-ID | <mailman.4278.1325360653.27778.python-list@python.org> |
| In reply to | #18263 |
On 12/31/11 7:34 PM, Lie Ryan wrote:
> On 01/01/2012 05:44 AM, davidfx wrote:
>> Thanks for your response. I know the following code is not going to be correct
>> but I want to show you what I was thinking.
>>
>> formatter = "%r %r %r %r"
>>
>> print formatter % (1, 2, 3, 4)
>>
>> What is the .format version of this concept?
>>
>
> I don't think the (%r)epr-formatting exist anymore, so if you want to do that
> you'll need to call repr manually.
Yes, it does.
formatter = '{!r} {!r} {!r} {!r}'
print formatter.format(1,2,3,4)
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2011-12-31 16:09 -0500 |
| Message-ID | <mailman.4282.1325365772.27778.python-list@python.org> |
| In reply to | #18263 |
On 12/31/2011 2:24 PM, Tim Chase wrote:
> On 12/31/11 12:57, Benjamin Kaplan wrote:
>> format is a method of the string class. You store the string the same way
>> you would any other.
>>
>> formatter = "Hello, {}"
>> print(formatter.format("world"))
>
> Just to note that this syntax doesn't quite work in some earlier
> versions (tested below in 2.6, which came stock in Debian Stable)
Autonumbering of {} was introduced, at my suggestion, in 3.1 and
included with 2.7 (I presume).
>
> >>> formatter = "Hello, {}"
> >>> print(formatter.format("world"))
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> ValueError: zero length field name in format
>
>
> It needs to be spelled "{0}"
>
> -tkc
>
>
--
Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2012-01-02 14:00 -0800 |
| Message-ID | <mailman.4321.1325544682.27778.python-list@python.org> |
| In reply to | #18258 |
Andrew Berg wrote: > On 12/31/2011 12:19 PM, davidfx wrote: >> Should we always be using .format() for formatting strings or %? >> > %-style formatting will eventually go away, but > probably not for a long time. %-style formatting isn't going away. ~Ethan~
[toc] | [prev] | [next] | [standalone]
| From | Rick Johnson <rantingrickjohnson@gmail.com> |
|---|---|
| Date | 2012-01-02 17:59 -0800 |
| Message-ID | <fd8933b8-0cca-4127-8f9b-38b056ee075a@h12g2000yqg.googlegroups.com> |
| In reply to | #18353 |
On Jan 2, 4:00 pm, Ethan Furman <et...@stoneleaf.us> wrote: > %-style formatting isn't going away. You may want to freshen up on the definition of "deprecation". If it was NOT going away, why the need to deprecate it? hmm? It would be more beneficial if you DO NOT encourage continued usage of this "end- of-life" feature. http://en.wikipedia.org/wiki/Deprecation
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-01-03 03:52 +0000 |
| Message-ID | <4f027b79$0$29880$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #18362 |
On Mon, 02 Jan 2012 17:59:43 -0800, Rick Johnson wrote: > On Jan 2, 4:00 pm, Ethan Furman <et...@stoneleaf.us> wrote: >> %-style formatting isn't going away. > > You may want to freshen up on the definition of "deprecation". I'm sure Ethan knows the definition of deprecation. I'm sure he also knows that % formatting is NOT deprecated. Please stop spreading FUD about Python features. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-01-02 22:58 -0700 |
| Message-ID | <mailman.4334.1325570337.27778.python-list@python.org> |
| In reply to | #18369 |
On Mon, Jan 2, 2012 at 8:52 PM, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > On Mon, 02 Jan 2012 17:59:43 -0800, Rick Johnson wrote: > >> On Jan 2, 4:00 pm, Ethan Furman <et...@stoneleaf.us> wrote: >>> %-style formatting isn't going away. >> >> You may want to freshen up on the definition of "deprecation". > > I'm sure Ethan knows the definition of deprecation. I'm sure he also > knows that % formatting is NOT deprecated. Please stop spreading FUD > about Python features. I can't believe I'm taking Rick's side here, but the docs do say: "Note: The formatting operations described here are obsolete and may go away in future versions of Python. Use the new String Formatting in new code." http://docs.python.org/py3k/library/stdtypes.html#old-string-formatting-operations I consider that a statement of deprecation, even if it doesn't use the term explicitly or describe a definite timeline for removal. Cheers, Ian
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-01-03 06:15 +0000 |
| Message-ID | <4f029d1e$0$29880$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #18377 |
On Mon, 02 Jan 2012 22:58:23 -0700, Ian Kelly wrote: > On Mon, Jan 2, 2012 at 8:52 PM, Steven D'Aprano > <steve+comp.lang.python@pearwood.info> wrote: >> On Mon, 02 Jan 2012 17:59:43 -0800, Rick Johnson wrote: >> >>> On Jan 2, 4:00 pm, Ethan Furman <et...@stoneleaf.us> wrote: >>>> %-style formatting isn't going away. >>> >>> You may want to freshen up on the definition of "deprecation". >> >> I'm sure Ethan knows the definition of deprecation. I'm sure he also >> knows that % formatting is NOT deprecated. Please stop spreading FUD >> about Python features. > > I can't believe I'm taking Rick's side here, but the docs do say: > > "Note: The formatting operations described here are obsolete and may go > away in future versions of Python. Use the new String Formatting in new > code." > > http://docs.python.org/py3k/library/stdtypes.html#old-string-formatting- operations > > I consider that a statement of deprecation, even if it doesn't use the > term explicitly or describe a definite timeline for removal. Which is exactly why it is not deprecated: it doesn't say it is deprecated and has no timeline for removal. It may not even be removed: "may" go away is not "will" go away. Going around saying that features are deprecated just because they may (or may not) some day in the distant future become deprecated is FUD. It simply isn't true that % formatting is deprecated: Python Dev has a formal process for deprecating code, and that process has not been applied to % and there are no plans to do so in the foreseeable future. There is a huge code base using this feature, including the standard library, and Python does not arbitrarily deprecate features used by real code without good reason. Just because a number of Python devs want to encourage people to use format doesn't imply that % will go away any time before Python 4000. -- Steven
[toc] | [prev] | [next] | [standalone]
Page 1 of 3 [1] 2 3 Next page →
Back to top | Article view | comp.lang.python
csiph-web