This lesson provides an overview of the powerful and flexible Python string formatting methods. Both literal and variable strings may be formatted.
- [Narrator] Python has rich string formatting capabilities. Here in Komodo, I've opened a working copy of Hello.py from chapter 11 of the exercise files. Python 3 uses a string method, called format, to format strings, and it works like this. If I create a variable, X equals 42, and here in the string I can say, "The number is," and I use these curly braces for a placeholder, and I type format, and give format the variable X, and when I run it, you'll notice it says, "The number is 42." So, in some languages, they call this string interpolation, but here, the method of the madness is exposed by allowing us to use the format method, like that.
The curly brackets are a placeholder, and the format method specifies values for the placeholder. If you have multiple values, say Y is 73, and I want to give another number here, I can specify the second value in format with a comma, and when I run it, you see it displays both numbers. You can name your variables, like this, xx, and bb, and then in the format, you can say xx equals, and bb equals.
And when I run this, you see our result is the same. Or, I can change them around, call this one xx, and this one bb, and it'll change them around in the results. Likewise, you can use positional arguments, say one and zero. The positional arguments start at zero, so here I'm reversing them again, and when I run this, you see it's still 73 and 42, but if I change them around, now it's 42 and 73 in our result.
And this even allows me to repeat a value if I like. And run this, I have 42, 73, and 42. Now, again, without anything specified, it simply takes the arguments in order, and so there's our two arguments. You can also add formatting instructions. Formatting instructions are preceded by a colon, and of course, if you had any positional arguments, you would put that before the colon, so I could say zero and one colon, like that, and I can say that I want this one left justified with five spaces, and this one right justified with five spaces, and when I run that, you see we have all kinds of spaces between them.
So, the number is the number of spaces including the value, and if I want to, I can add a leading zero, and you see now I have five positions, two of them are taken up by the value, and the other three are filled with zero. I can add a sign if I want to, and now that'll have a sign. Even if it's positive, it'll still give me the sign, and I can use that in combination with the positional argument, or even with the leading zeros, and all of that still works.
You'll notice that the sign takes up one of the five positions. Okay, let's go back to using one value here, and I'm going to call this 42 times seven four seven, because I want to get a nice big number. So, when I run this, you notice I have a nice big number. If I want to format that with commas, I simply put in a colon for the formatting instructions, and add a comma, and now I have the comma for a thousands separation. And I can make that a bigger number if I want to, and you notice that it just continues to use the commas properly.
And, if I'm European, and I want to use periods instead of commas, I can always use the replace method, and replace the comma with the period, and we run this. Oh, not enough parentheses. There we go. And the number is now formatted with periods instead of commas. You may also specify a fixed number of decimal places with the letter F. So if I run this, you notice it defaults to that many decimal places, or I can say point three, and run it again, and now it gives me three places to the right of the decimal point.
You can specify different bases. I'm going to put this back to the number 42, and we'll print this in hexadecimal, which is 2a, or octal, which is 52, or binary, which is one zero, one zero, one zero. Beginning with Python version 3.6, you may now use F strings for formatting. And so this is instead of this whole format thing here, and it looks like that.
So, I put the letter F, either lowercase or uppercase, but I prefer the lowercase, in front of the string, and then I use the variable name where you would otherwise use the positional argument with the format method. And so, when I run this, its number 42. And if I want to do some formatting, I can say dot three F, and it'll give me three decimal places. All the formatting methods that I've already shown you will work with this method as well.
In fact, the F string is simply a shortcut for the format method. I tend to prefer the F strings, and I expect that they will quickly become commonplace for Python 3 code. For more details on string formatting in Python, Python.org has a full reference here. Click on format string syntax, and this is complete documentation. It's a little bit techy language, but it's all there.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api