FAQ: Learn Python: Classes - Constructors

In the solution to this section there is the following print statement:

print(“New circle with diameter: {diameter}”.format(diameter=diameter))

why in the format method part does it have (diameter=diameter), instead of just (diameter)

Because str.format() does not support variables, only keywords. We declare the keywords in the format argument.

"Lorem ipsum {dolor} sit amet.".format(dolor=variable)

In version 3.6 and above, f-string is supported, which in turn supports variable interpolation, directly.

print (f"Lorem ipsum {variable} sit amet.")

I don’t think it’s used much (likely because it’s harder to read) but there is a route to use positional arguments (even alongside keyword arguments), the main use I’ve come across is when you might want to repeat certain arguments by positional index and mix up their order-

twister = """\ She sells {0}{1} on the {0} shore, the {1} she sells are {0}{1} I'm sure... """.format('sea', 'shells') print(twister)

You can also just supply an equal number of placeholders to positional arguments, e.g.

this_string = "{}, where's the {}"
print(this_string.format('David', 'baby'))

I think the f-string version is much nicer in that second instance though and even the first one risks becoming very brittle and hard to follow. Keywords tend to be a nicer option.

1 Like

Yeah, that is all familiar stuff. Once f-string became a thing, pretty much stopped using str.format() altogether, much the same way I quit using modulo formatting once we learned about str.format(). Each iteration has been hugely superior to its predecessor.

1 Like

I’ve looked at this again and think I may understand the code but let me run by you what I think it means.
In the line “New circle with diameter: {diameter}”.format(diameter=diameter)), the first “diameter” is the
keyword which is set equal to the value of “diameter”. If it was
print(“New circle with diameter: {diameter}”.format(diameter)), "diameter would be interpreted as a variable name
which you said .str.format() does not support variables. Have I got it ?


Correct. Keywords are not the same as variables since they exist only in the confines of the construct (the string interpolation template).

>>> diameter = 10
>>> print ("New circle with {diameter}".format(diameter))
Traceback (most recent call last):
  File "<pyshell#102>", line 1, in <module>
    print ("New circle with {diameter}".format(diameter))
KeyError: 'diameter'

Either leave the template empty, {} or insert a position number, {0}, or define the keyword, `"{d}".format(d=diameter)

Scroll back up this topic and see if you can find a link to pyformat. It gives a complete rundown of how to use modulo formatting (the old) and .format (then new). It does not yet describe f-string, but I’m sure it will, eventually. There are plenty of sites that explain it, including the docs post v3.5.

f"Lorem ipsum {dolor:directives}"
                /        \
!          variable    formatting