How's it going? Let's talk about Python's f-strings.
You're probably using them already, but they have a few cool party tricks up their sleeve that can make your code cleaner and your life a whole lot easier.
We're going to cover five of my favorites. Let's just dive right in.
1. Making Huge Numbers Readable
Okay, so you've got this giant number...
n = 1000000000
It's just... a wall of zeros. Hard to read, right? You might already know you can manually add underscores like 1_000_000_000 to make it readable for you, and Python just ignores them.
But what if the number comes from a variable? Instead of just printing it, you can use an f-string to format it on the fly.
Check this out:
n = 1000000000
print(f'{n:_}') # Output: 1_000_000_000
print(f'{n:,}') # Output: 1,000,000,000
By adding :_ or :, after the variable, you can automatically add underscores or commas as thousand separators. It's a small thing, but it makes your output so much more professional and easy to scan.
Unfortunately, those are the only two separators Python gives you out of the box. If you want to use a space or something else, you'd have to write your own little function for it.
2. Aligning Text Like a Pro
Ever tried to print a neat little table or align some text in your console output? It can be a pain.
F-strings make this surprisingly simple.
Imagine you have a short string, but you want it to take up a specific amount of space, say 20 characters. You can align it to the right, left, or center within that space.
variable = "hey"
# Right-align (17 spaces, then "hey")
print(f'|{variable:>20}|')
# Left-align (the default, but good to know)
print(f'|{variable:<20}|')
# Center-align
print(f'|{variable:^20}|')
This will give you:
| hey|
|hey |
| hey |
But the fun doesn't stop there. You can also specify a "fill" character to use instead of empty space.
print(f'|{variable:_>20}|') # Fills with underscores
print(f'|{variable:#<20}|') # Fills with hashes
print(f'|{variable:|^20}|') # Fills with pipes
And you get this crazy-looking (but sometimes useful!) output:
|_________________hey|
|hey#################|
|////////hey/////////|
3. Quick and Easy Date Formatting
Working with dates can be... messy. You get this big, clunky datetime object, and you only want to show the day and month.
F-strings have built-in support for date formatting codes.
First, let's grab the current time. Whoops, I always make this mistake... you have to import datetime from the datetime module.
from datetime import datetime
now = datetime.now()
Now, you can format it directly inside an f-string using specifiers that start with a %.
# Format as Day-Month-Year
print(f'{now:%d-%m-%Y}') # Output: 19-10-2025
# Format as Hour:Minute:Second (24-hour)
print(f'{now:%H:%M:%S}') # Output: e.g., 10:30:55
# Get the full local date/time string
print(f'{now:%c}')
# Use 12-hour format with AM/PM
print(f'{now:%I:%M %p}') # Output: e.g., 10:30 AM
There's a whole bunch of these format codes. If you ever need more, just Google "strftime format codes" and you'll find a complete list.
4. Taming Those Wild Decimals
You know when you do a calculation and end up with a number like 1234.567890123? Most of the time, you don't need all that precision. It's just noise.
Sure, you can use the round() function, but you can also do it right inside your f-string.
number = 1234.5678
# Round to two decimal places
print(f'Result: {number:.2f}') # Output: Result: 1234.57
# Round to zero decimal places (an integer)
print(f'Result: {number:.0f}') # Output: Result: 1235
The .2f tells Python to format the number as a float with two decimal places. It even handles the rounding for you.
And here's something really cool: you can combine this with the thousand separator from our first trick.
# Add commas AND round to 3 decimal places
print(f'Result: {number:,.3f}') # Output: Result: 1,234.568
Now that's taking number formatting to the next level.
5. The Ultimate Debugging Shortcut (My Favorite)
This last one is my absolute favorite. We've all written code like this to debug something:
a = 5
b = 10
print(f'a + b = {a + b}')
It's descriptive, but it feels a little repetitive to type a + b twice. As a developer, I'm professionally lazy... I don't want to type six extra characters if I don't have to.
Well, check out this beautiful syntax:
a = 5
b = 10
print(f'{a + b = }')
That's it. Just add an equals sign (=) inside the curly braces. Python is smart enough to print the expression itself, followed by an equals sign, and then the result.
The output is exactly what you want: a + b = 15.
This is a game-changer for quick debugging. It works with anything, not just math.
my_var = "Bob says hi"
print(f'{my_var = }')
# Output: my_var = 'Bob says hi'
print(f'{bool(a) = }')
# Output: bool(a) = True
The best part is, if you change the variable name, you don't have to remember to change it in your print string, too. It saves you from those silly mistakes.
And that's a wrap! Five f-string tricks that I use all the time. They're a ton of fun to play with and can seriously speed up your workflow.
I definitely recommend committing a few of these to memory.
Thanks for hanging out, and I'll see you in the next one.