Python's Not (Just) For Unicorns

An interactive introduction to programming in Python, for human beings and whoever else

Chapter 25

Formatting output with f strings

I have to admit something: Python invented motorcycles a while ago, but I’ve only been teaching you to ride a bike. The guilt has finally gotten to me, so I’m coming clean and sharing a secret.

You know how we’ve been using print to print things out? And putting a comma between the sections?

name = 'Laksmi'
print('Hello, my name is', name)

It’s definitely not the best way to do it - it’s old school, it’s inflexible, and just… people do it, it works, but it’s just not modern.

As of Python 3.6, there is a cool fun new thing called f strings! It’s the one new feature we might actually use. Instead of using the comma weirdness, we can do this:

name = 'Laksmi'
print(f'Hello, my name is {name}')

Two things to notice:

  • The string starts with f', not just ' (f" works too)
  • The variable is inside of curly braces {}

It works just like a fill-in-the-blank. The f before the string says “hey, we’re going to put some variables in here!” and the {name} says “put the name variable in here!” and then everyone is happy and we didn’t have to use a hundred commands.

Python’s f-strings also work with numbers, and even math!

minutes = 2.5
print(f'You will have to wait {minutes * 60} seconds')

Instead of calculating a new seconds variable, we just did the math inside of the curly braces! And if you think that’s amazing, maybe you should just skip the next section, because your life is about to change forever.

When you’re using your variable, you can also add weird little modifiers to tell Python how you want the variable displayed. It’s mostly used with numbres - you can add commas to big numbers, round long decimal numbers, left- or right-align, or a hundred other things!

years = 65000000
print(f'Tyrannosaurus rex roamed the earth {years:,} years ago')

pi_value = 3.14159265
print(f'The value of pi is roughly {pi_value:.3f}')

id_number = 3
print(f'Your ID number is {id_number:04}')

As long as we always put an f before you start the string, we’ll always be able to use these nice fill-in-the-blanks.

Okay. Whew. Our minds have been blown out into space, but it might be worth trying this out on our own real quick. Remember when we printed out the populations of the cities last chapter? Let’s try to reproduce it agian, but with f strings.

Let’s print out the name of the city and the population, but we’ll use the long number and format it with commas. For example, it should start with “Santiago has a population of 7,200,000”.

populations = {
  'Santiago': 7.2,
  'San Diego': 1.407,
  'Nara': 1.401,
  'Moscow': 12.19
for key, value in populations.items():
  print('The city', key, 'has a population of', value)
  • Hint: You should first change the print statement to use an f-string
  • Hint: F-strings need two things - an f at the beginning, and {} around any variables used inside.
  • Hint: Yes, it’s okay to have two variables in the same f string
  • Hint: It should look like f'The city {key} has a population of {value}'
  • Hint: You’ll want to multiply the population by 1000000 to turn it from millions into the “real” number
  • Hint: You can make a separate variable for the long version of the population or you can do the math in the {}
  • Hint: To convert it from millions, you’ll want to do {value * 1000000}
  • Hint: 7200000 needs some commas! Check the example above to see how we did it when talking about dinosaurs.
  • Hint: If you’re doing math in the curly brace, the :, goes before the }, not after the variable name.
  • Hint: Try out `{value * 1000000:,}
  • Hint: 7,200,000.0 looks kind of weird, we don’t need that decimal. We rounded to two decimal places with pi up above, how do we round to zero decimal places?
  • Hint: If we made a separate variable, we could have used int()! But let’s practice the f-string way.
  • Hint: :.0,} doesn’t work, and :,:.0} doesn’t either… but ,.0} does!
  • Hint: Our final f-string might be f'The city {key} has a population of {value * 1000000:,.0}'

Chapter summary

We learned about f strings, which are a nicer way to print and format strings. They work like fill-in-the-blanks with our variables, and all you need to do is add a f at the beginning of the string!

F-strings also let us do fun tricks like rounding and adding commas if we have long decimals or large numbers.