What Is Output Formatting in Python and How Do You Do It?

Output formatting is all about controlling how data or information, generated by a program, is displayed or outputted. With well-thought-out formatting, output is more readable, organized and user-friendly. To achieve proper output formatting, one must consider layout, alignment, precision, spacing and sometimes color or style to improve comprehension and presentation.
Each programming language has its own special method of dealing with output formatting, and Python is no exception.
I want to introduce you to some various types of output formatting so you can get an idea of how it works within Python. In this guide, we’ll deal with string interpolation, string concatenation, number formatting, precision formatting with f-strings and float formatting with f-strings. That should cover a lot of ground for your Python output formatting.
Fortunately, this is a concept that shouldn’t challenge you all that much.
Ready for the world of formatting, Sheila? Deedle-deedle-loo.
Let’s get our formatting on.
String Interpolation
String interpolation allows you to embed variables, expressions or function outputs directly within a string literal. When the string is evaluated, the placeholders are replaced with their corresponding embedded values to produce a final string with the proper values.
Here’s a simple example of using string interpolation in Python.
As you can see, we have two placeholders, one for name and one for age. We’ve also assigned name and age as variables. When the Python script goes to run the print statement, it automatically replaces name and age with the assigned values.
The output of the script will be:
String interpolation is a simple concept that you’ll use extensively in your Python scripts and apps.
String Concatenation
String concatenation uses the + operators to join strings together. This works similarly to string interpolation, but instead of using curly braces, it combines those values together by using +.
Here’s a simple example:
A few things about that. First, we have three strings:
- “Hello, my name is “
- ” and I am “
- ” years old.”
Next, we have our variables, which are name and age. But notice that with age, it uses str(age), which informs Python what age is. Also, notice the spaces in the strings, such as ” and.” That ensures there’s a proper space in the output.
Number Formatting
Python has a few methods used to format numbers, and each method uses the % operator. These are important because you need to know how your numbers need to be formatted. For example, is the number an integer, a floating point number, lowercase hexadecimal or uppercase hexadecimal? If you were to format a number that should be an integer as a floating point number, the output could be incorrect.
The operators are as such:
- %d: integer
- %f: floating point number
- %x: hexadecimal (lowercase)
- %X: hexadecimal (uppercase)
How does this work? Here’s an example:
The output of the above would be:
But what if we used %f (for floating point number), like so:
The output of the above would be:
It gets a bit odd when you go with the %x (lower case hexadecimal), like this:
The output would then be:
So, as you can see, number output formatting is very important.
Precision Formatting With F-Strings
Formatting with f-strings came about in Python 3.6 and allows you to format numbers with more precision than the previous example.
F-strings (aka, formatted string literals) are used to embed expressions inside string literals in Python. F-strings have a minimal syntax and use ‘f’ or ‘F.’ You can include variables or expressions within curly braces, which are evaluated at runtime, for concise and efficient f formatting.
Here’s an example of precision formatting with f-strings, using Pi:
The output would be:
The f-string in the above is f”{pi:.2e}”.
The format specifier .2e is used to display a floating-point number in scientific notation with two digits after the decimal point. To indicate that in the output, the lowercase e is shown and the .2 means two digits will be shown after the decimal point.
Here are some other examples that illustrate how to do different formatting with f-strings:
- Fixed decimal places: f”{pi:.3f}” → 3.142
- Field width and alignment: f”{pi:10.2f}” → ‘ 3.14’ (right-aligned in a 10-character field)
- Left, right and center alignment: f”{pi:<10.2f}” (left), f”{pi:>10.2f}” (right), f”{pi:^10.2f}” (center)
This only scratches the surface of output formatting in Python, but it gives you a good starting point. Just remember, formatting your output properly can make the difference between a well-designed and written Python application and one that isn’t.