Elegant Ways to Manipulate Print Outputs in Python
If you’re anything like me, you’ve probably used multiple print() statements to add blank lines between outputs. But as I’ve discovered, there are more elegant ways to manipulate print outputs in Python! Let me show you some of the most helpful techniques I’ve found.
The Old Way
Here’s how I used to do it:
command = 'show ap summary'
print()
print(command)
print()
This gives us the output:
show ap summary
It accomplishes the goal of adding blank lines before and after the output, but it always bugged me that I needed three lines of code to do it.
Enter the Escape Character
I eventually discovered the concept of escape characters in Python, specifically the \n escape character. When \n is included in a string, Python interprets it as a new line. This allows us to achieve the same result more concisely:
command = 'show ap summary'
print('\n' + command + '\n')
This method, called string concatenation, accomplishes our goal in a single line. However, I’ve never been a big fan of concatenation because it can become unwieldy with more complex strings.
The F-string: A Game Changer
Now, let me introduce you to my new favorite thing in Python: the f-string! It simplifies printing strings and numbers in the way we want. Here’s how to use an f-string for our example:
command = 'show ap summary'
print(f'\n{command}\n')
To create an f-string, you simply put an ‘f’ before the opening quotation mark. The output is exactly what we want:
show ap summary
F-strings allow you to include variables and expressions directly in the string by placing them inside curly brackets {}. This works for:
- Variables
- Calculations
- Formatting
- Methods
- Nested structures
- And more!
Advanced F-string Examples
Here are some more advanced examples of what you can do with f-strings:
Integer Formatting
n = 1000000000
print(f'{n:_}') # Output: 1_000_000_000
print(f'{n:,}') # Output: 1,000,000,000
String Alignment
var = 'variable here'
print(f'{var:>30}:') # Right-aligned
print(f'{var:30}:') # Right-aligned with '_' filler
print(f'{var:#<30}:') # Left-aligned with '#' filler
print(f'{var:|^30}:') # Centered with '|' filler
Date and Time Formatting
from datetime import datetime
now = datetime.now()
print(f'{now:%m.%d.%y}') # Output: 07.20.24
print(f'{now:%H:%M:%S}') # Output: 20:31:36
print(f'{now:%c}') # Output: Sat Jul 20 20:31:36 2024
print(f'{now:%I%p}') # Output: 08PM
Number Manipulation
n = 1234.5678
print(f'Rounding result: {n:.2f}') # Output: 1234.57
print(f'Rounding result: {n:.0f}') # Output: 1235
print(f'Rounding result: {n:,.3f}') # Output: 1,234.568
print(f'Rounding result: {n:_.0f}') # Output: 1_235
Variable Printing
a = 5
b = 10
my_var = 'Tot says hi'
print(f'{a + b = }') # Output: a + b = 15
print(f'{bool(a) = }') # Output: bool(a) = True
print(f'{my_var = }') # Output: my_var = 'Tot says hi'
These examples demonstrate the versatility and power of f-strings in Python. They allow for cleaner, more readable code while providing powerful formatting options. Let me know what you think, happy coding!

Leave a comment