Question about ex6.py - Unidentified Flying Brackets

Oh yeah, what’s the bracket doing in line 22?

ex6 question

To the best of my understanding it’s a way of showing the string where something can be formatted in. Then, when two lines later the hilarious variable is formatted in, Python knows to dump it into those curly braces (or whatever you call them).

Fair enough. That was my System One brain assumption (Dual Processing psychology theory), good to hear some confirmation though!

When you use the .format() function you’re using a slightly different style of formatting strings. It’s like the f-string, but just uses a Python function to do the work. When you do that, you can use an empty {} to mean “a parameter to .format() at this point”. So if you called “{} {} {}”.format(x,y,z) then x goes in the first one, then y, then z.

Another way to think of it is the {} are holes you punched in the string, and .format() fills them in.

1 Like

Formatting strings in python are a little tricky to learn, because there’s two types. An older style using % and another newer one using {}. There’s quite a bit you can do with string formatting. There’s an open-sourced webpage that I like to use, whenever I want to lookup how to format my string in python.

Check out : https://pyformat.info/

1 Like