Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

Learning objectives

By the end of this section you should be able to

  • Use f-strings to simplify output with multiple values.
  • Format numbers with leading zeros and fixed precision.


A formatted string literal (or f-string) is a string literal that is prefixed with "f" or "F". A replacement field is an expression in curly braces ({}) inside an f-string. Ex: The string f"Good morning, {first} {last}!" has two replacement fields: one for a first name, and one for a last name. F-strings provide a convenient way to combine multiple values into one string.


Printing an f-string

Concepts in Practice

Basic f-strings

What is the output of the following code?
animal = "dog"
says = "bark"
print(f"My {animal} says {says} {says} {says}")
  1. My dog bark bark bark bark
  2. My dog says bark bark bark
  3. Error
What is the output of the following code?
temp = "hot"
food = "potato"
print("{temp} {food}")
  1. {temp} {food}
  2. hot potato
  3. Error
How can the following code be rewritten using an f-string?
print(x, "+", y, "=", x + y)
  1. print(f"x + y = {x+y}")
  2. print(f"{x + y} = {x + y}")
  3. print(f"{x} + {y} = {x+y}")

Formatting numbers

Programs often need to display numbers in a specific format. Ex: When displaying the time, minutes are formatted as two-digit integers. If the hour is 9 and the minute is 5, then the time is "9:05" (not "9:5").

In an f-string, a replacement field may include a format specifier introduced by a colon. A format specifier defines how a value should be formatted for display. Ex: In the string f"{hour}:{minute:02d}", the format specifier for minute is 02d.

Format Description Example Result


Decimal integer (default integer format).




Decimal integer, with comma separators.




Decimal integer, at least 10 characters wide.


'  12345678'


Decimal integer, at least 10 characters wide, with leading zeros.




Fixed-point (default is 6 decimal places).




Fixed-point, rounded to 4 decimal places.




Fixed-point, rounded to 4 decimal places, at least 8 characters wide.


'  3.1416'


Fixed-point, rounded to 4 decimal places, at least 8 characters wide, with leading zeros.



Table 3.3 Example format specifiers. The table shows common ways that numbers can be formatted. Many more formatting options are available and described in Python's Format Specification Mini-Language.

Concepts in Practice

Formatting numbers

What f-string formats a date as MM/DD/YYYY?
  1. f"{month:02d}/{day:02d}/{year}"
  2. f"{month:2d}/{day:2d}/{year}"
  3. f"{month}/{day}/{year}"
What statement displays the variable money rounded to two decimal places?
  1. print(f"{money:2d}")
  2. print(f"{money:2f}")
  3. print(f"{money:.2f}")
What format specifier displays a floating-point number with comma separators and rounded to two decimal places?
  1. .2,f
  2. ,.2f
  3. ,d.2f

Try It

Mad lib (f-string)

A mad lib is a funny story that uses words provided by a user. The following mad lib is based on four words (user input in bold):

    Enter a name: 
Enter a noun:
Enter an adjective:
Verb ending in -ing:
Buster, the super dog, likes to go swimming.

Most of the code for this mad lib is already written. Complete the code below by writing the f-string.

Try It

Wage calculator

You just landed a part-time job and would like to calculate how much money you will earn. Write a program that inputs the time you start working, the time you stop working, and your hourly pay rate (example input in bold):

    Starting hour: 
Starting minute:
Stopping hour:
Stopping minute:
Hourly rate:

Based on the user input, your program should calculate and display the following results:

    Worked 9:30 to 11:00
    Total hours: 1.5
    Payment: $22.50

For this exercise, you need to write code that (1) calculates the total payment and (2) formats the three output lines. Use f-strings and format specifiers to display two-digit minutes, one decimal place for hours, and two decimal places for payment. The input code has been provided as a starting point.

Assume the use of a 24-hour clock. Ex: 16:15 is used instead of 4:15pm.


This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at
Citation information

© Feb 26, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.