Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > .format vs. %

Reply
Thread Tools

.format vs. %

 
 
davidfx
Guest
Posts: n/a
 
      12-31-2011
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
 
Reply With Quote
 
 
 
 
Yaşar Arabacı
Guest
Posts: n/a
 
      12-31-2011
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",gende r="female")

Sat, 31 Dec 2011 20:19:34 +0200 tarihinde davidfx <(E-Mail Removed)>
şö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
 
Reply With Quote
 
 
 
 
Andrew Berg
Guest
Posts: n/a
 
      12-31-2011
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
 
Reply With Quote
 
davidfx
Guest
Posts: n/a
 
      12-31-2011
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?

 
Reply With Quote
 
davidfx
Guest
Posts: n/a
 
      12-31-2011
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?

 
Reply With Quote
 
Evan Driscoll
Guest
Posts: n/a
 
      12-31-2011
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 becorrect 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?
>




-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJO/1iuAAoJEAOzoR8eZTzg4jIH/3pDVFzuwDrMG7A0vojG1B8H
oGxhRb/5lGlpGxIQIU68+isGiO+WFnkCRBhIJYtemEuMyGHqcSBYTH5Mw NZ4Gfl3
Spy4MmOamIR4YzXj3/cR/8LNft0e8ZK4aOiOlmpFcGdSV/PDjPhaBlkzEhm+aHQC
y6ZMTXd7BiH18ERX2bkZGPF6O+9Sij5aVKyKSkEbH6eV89E/Sy06jEVI0lUeK8xj
+iTJBT4PCHhcIfhQEGLM7hzKQVqrI1z8IlJaMFrzolMRKy6hRt bEz80UwStMTPwc
/dqfG10p5ovvWjk1oaT1UMRtgymn8N1jC4gPecoq/kPuovqkZZnRDoE2CqieUes=
=0YDi
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Yaşar Arabacı
Guest
Posts: n/a
 
      12-31-2011
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 <(E-Mail Removed)>
şö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
 
Reply With Quote
 
Alexander Kapps
Guest
Posts: n/a
 
      12-31-2011
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).
 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      12-31-2011
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


 
Reply With Quote
 
Lie Ryan
Guest
Posts: n/a
 
      12-31-2011
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments