Joe Neville
Posted on February 12, 2021
✍️ In my previous post about Python f-strings, I was a little too hard on the older string formatting approach, format().
👀 I was focused solely the string formatting in terms of string substitution, for which I find f-strings the more intuitive and superior approach.
🧰 However, as some devs rightly pointed out, format has some other, very useful, features, and should not be consigned to the mental dustbin. But rather dusted off and placed alongside f-strings in your Python toolkit when it comes to strings.
format() uses
👉 Formatting strings with variable substitution and concatenation
a = "know"
b = "thanks!"
quote = "Yes, we already {} this, {} {}".format(a, b)
print(quote)
Yes, we already know this, thanks!
So far, so f-strings.
👉 String formatting with padding
If you are struggling to structure your string lay-out and presenting them in a way that is pleasing to the eye, format can help. Rather than having to count out 26 whitespaces each time (or was it 25) to get them to line up, you can use padding.
Rather than just use empty curly braces, padding allows you to:
- define a field size to be inserted into the string
- position your characters
- define the fill characters.
In the curly braces, to the right of a colon, add the minimum number of character fields: {:20}
this will be 20 fields minimum.
print("Give me some {:20} please!".format("space"))
Give me some space please!
Use "<" to left-align, ">" to right align and "=" to centre. The default is to left-align, as above:
print("Give me some {:>20} please!".format("space"))
Give me some space please!
print("Give me some {:^20} please!".format("space"))
Give me some space please!
Add in a character to fill the designed number of spaces, the default is just whitespace:
print("Give me some {:*^20} please!".format("space"))
Give me some *******space******** please!
Note that if you want to use your own fill characters, you need to set the alignment as well, not rely on the default. Thus for left-align:
# This will throw an error
print("Give me some {:*20} please!".format("space"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Invalid format specifier
# Define the alignment
print("Give me some {:*<20} please!".format("space"))
Give me some space*************** please!
In fact, format uses a "mini-language" that has numerous other options. Check the Python docs for more info.
There's going to be some uses for padding, but if you are trying to align your text into a table, there are Python libraries for that, which are probably a better option, like tabulate.
👉 Accessing Dictionary Values
If you're working with Python dictionaries, format has another useful feature for you. You can access the values of a dictionary directly from format. No need to unpack to variables then reference them, you can call the dictionary inside the format argument.
paint = {"colour": "Sky Blue", "volume": 5}
"For sale, {volume} litres of {colour} paint".format(**paint)
'For sale, 5 litres of Sky Blue paint'
👉 Check this docs for more
format() has even more features that those listed here, check the Python docs for more examples.
Also, I've used this blog from Digital Ocean a few times. It presents information about format() in a style that I found easier to understand than diving straight in the official docs.
In conclusion, f-strings and format(); group hug. 🤗
Posted on February 12, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 26, 2024