.format vs. %

D

davidfx

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
 
Y

Yaşar Arabacı

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")
 
A

Andrew Berg

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?
 
D

davidfx

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?
 
D

davidfx

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?
 
E

Evan Driscoll

How 'bout just:
'1 2 3 4'

Evan


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+WFnkCRBhIJYtemEuMyGHqcSBYTH5MwNZ4Gfl3
Spy4MmOamIR4YzXj3/cR/8LNft0e8ZK4aOiOlmpFcGdSV/PDjPhaBlkzEhm+aHQC
y6ZMTXd7BiH18ERX2bkZGPF6O+9Sij5aVKyKSkEbH6eV89E/Sy06jEVI0lUeK8xj
+iTJBT4PCHhcIfhQEGLM7hzKQVqrI1z8IlJaMFrzolMRKy6hRtbEz80UwStMTPwc
/dqfG10p5ovvWjk1oaT1UMRtgymn8N1jC4gPecoq/kPuovqkZZnRDoE2CqieUes=
=0YDi
-----END PGP SIGNATURE-----
 
Y

Yaşar Arabacı

You mean like this?
==========================='I like myself'
============================
 
A

Alexander Kapps

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).
 
T

Tim Chase

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)
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
 
L

Lie Ryan

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.
 
R

Robert Kern

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
 
T

Terry Reedy

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).
 
T

Terry Reedy

'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.
 
R

Rick Johnson

Hello everyone,
I just have a quick question about .format and %r %s %d.

Should we always be using .format() for formatting strings or %?

ALWAYS use the format method over the old and dumpy string
interpolation. Why? Well because the format method is so much more
powerful, elegant, and the way of the future. Some might argue that
format is slower than interpolation (and i can't comment in favor of
either) HOWEVER, if speed is your concern than interpolation is NOT
the answer.
If I wanted to put .format into a variable, how would I do that.

That question has been answered many times in this thread already.

You may find the format spec to be cryptic at first. Well, most find
regexes cryptic also -- but would anyone recommend NOT using regexes
just because of crypti-ness? I think not. It's a non-starter.
 
S

Steven D'Aprano

You may find the format spec to be cryptic at first. Well, most find
regexes cryptic also -- but would anyone recommend NOT using regexes
just because of crypti-ness? I think not. It's a non-starter.

I would.

If you have a task that doesn't *need* a regular expression solution,
don't use a regular expression.


For what it's worth, I like the syntax of % formatting. It's nice and
simple and compact while still being readable. format() is more powerful,
but when I don't need that power, I stick to % formatting.
 
S

Steven D'Aprano

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top