Formatting Strings
00:00 In the previous lesson I gave an overview of the course. In this lesson, I’ll be reviewing the various options for string formatting in Python and pointing out their limitations.
00:10 PEP 20 is the Zen of Python. It’s a poem-esque thing that describes the philosophy of good Python code and Python itself. One of the lines is “There should be one —and preferably only one—obvious way to do it.
00:23 Even Python, the language itself, sometimes struggles to follow the Zen of Python. There are currently three ways of formatting strings in your favorite language, and this is due to the evolution of the language over time.
00:35
The original mechanism is C-style formatting where a string uses a percent placeholder to indicate where something could be replaced. So %s
for a string and %f
for a float.
00:47
You then operate on this kind of string with another percent sign, handing it a tuple containing a value to insert against each of those markers. Python 2.6 added the str.format()
method, which instead of using percent as a placeholder uses curly brackets.
01:04
You call format()
on the string itself, kind of like you can do with .upper()
, .lower()
, and other string methods.
01:10
And the arguments you provide to format()
are what get used to help populate the string. And more recently, f-strings got added in Python 3.6.
01:20
This kind of string uses an f
as a prefix before the string to indicate its type. Like format()
, it also uses brace brackets, but instead of being placeholders, it takes variable names directly.
01:31 Anything in scope can be used in the brace brackets themselves. Let’s dip into the REPL to see these in practice.
01:39 First, I need some variables to play with,
01:45 and let’s start out with the C-style formatter.
01:53
Here, the %s
and %f
in the string are the placeholders to be replaced. The s
and f
indicate the kind of value conversion to take place corresponding to string and floating point.
02:06 After the format string, you use the percent sign to invoke the replacement process, handing it a tuple. The tuple needs to have the same number of things in it as the placeholders in the string.
02:18
The end result is a string with the placeholders replaced. You really shouldn’t use floats for money. They’re problematic when it comes to rounding, but ignoring that, what if I want 10.99
to only show two decimal places?
02:31
Well, you can add information to the %f
placeholder to specify the format of the result.
02:38
Putting the 0.2
between the percent and f
says to format the float to two decimal places, giving us 10.99
, a little more reasonably, without those trailing zeros.
02:55
One of the drawbacks of the C-style method is if your string uses a value more than once, you have to repeat the corresponding variable in the tuple. Python 2.6 added the str.format()
method to address this and in an attempt to make string formatting more readable.
03:17
Instead of using a percent placeholder, the format()
method uses brace brackets. The number in the brackets indicates the position of the argument to use for replacement.
03:28 I haven’t done it here, but I could put curly brace zero several times in the string, and each instance gets replaced by the zero argument or item variable.
03:38 Similar to the C-style, you can add format information. You can also use named values instead of account for the arguments.
03:54
To use a format specifier, here, you add a colon inside the braces with the kind of formatting coming after it. In this case, I’ve used .0f
to round 10.99
to no decimal places.
04:06
Quite honestly, I’ve never liked the format()
method. Part of that’s because I was super used to C-style, having used it in several other programming languages. Part of it was because it required more typing and the situation of argument reuse isn’t all that common. And last, it also isn’t as performant as the C-style.
04:25 That last reason is rationalization. It’s true, but if performance were my primary concern, I wouldn’t have been writing Python in the first place. Python 3.6 saw the addition of a third way to format strings, the f-string.
04:39 This one I like, and I switched to it quickly in my own code.
04:46
You indicate an f-string by prefixing the string with the letter f
, hence its name. Inside, like with format()
, you also use brace brackets, but unlike the format()
method, that’s all you need.
04:57 The Python grammar refers to the items in braces as fields. Any value that is in scope can be used in the field and gets replaced. A field can even handle function calls.
05:09 Fields also support the same format specifier as the previous method.
05:19
So once again, I can do .0
to round my price.
05:25 There are still a few things you can’t do easily with any of these three methods. In the next lesson, I’ll talk about their limitations as a prequel to why t-strings got created.
Become a Member to join the conversation.