Skip to content

3.5 Print with f strings

F-strings, or formatted string literals, are a feature introduced in Python 3.6 that provide a way to embed expressions inside string literals, using curly braces {}. The primary advantages of using f-strings are readability, performance, and ease of use.

Syntax:

The basic syntax for an f-string is to prefix the string with an f or F and include expressions inside curly braces {}.

name = "Jake"
f_string = f"Hello, {name}!"
 


 

3.5a Embedding Expressions

F-strings allow you to directly embed expressions within strings, making it easy to include variable values, the results of function calls, or even inline calculations.

Example 3.5.1 - Embed expressions within string

name = "Jake"
age = 14
message = f"Name: {name}, Age: {age}"
print(message) 
Example 3.5.1 - Output

Name: Jake, Age: 14

 

Example 3.5.2 - Embed expressions within string in the print() function

greeting_string = "Hello"
person_name = "Jamie"
print(f"String: {greeting_string} dear {person_name}") 
Example 3.5.2 - Output

Hello dear Jamie

 


 

3.5b Formatting Numbers:

You can format numbers directly within the string.

Example 3.5.3 - F-strings with formatting numbers

myfloat = 1.23456789
print(f"Float with 3 decimal places: {myfloat:.3f}")

Example 3.5.3 - Output

Float with 3 decimal places: 1.235

 

Example 3.5.4 - More f-strings examples with formatting numbers

PI = 3.14159
formatted_pi = f"Pi is approximately {PI:.2f}"
print(formatted_pi)

Example 3.5.4 - Output

Pi is approximately 3.14

The above expression {PI:.2f}inside an f-string is used to format the floating-point number pi to two decimal places. Here's a breakdown of what each part means:

  • PI - This is the variable whose value you want to format. (note that we're using uppercase letters for constant variable here)
  • : - This introduces the format specification.
  • .2f - This is the format specification itself, which has two components:
  • . - Indicates that you want to format the number to a specific number of decimal places.
  • 2 - Specifies that the number should be formatted to two decimal places.
  • f - Stands for "fixed-point number," meaning that the number will be presented as a floating-point number.

 


 

3.5c Additional formatting options

You can adjust the formatting in various ways. Here is another example.

Example 3.5.5 - In this example, {PI:10.3f} would format PI to 3.142 but with a minimum width of 10 characters, padded with space.

python PI = 3.14159 formatted_pi = f"Pi is approximately {PI:10.3f}" print(formatted_pi)

Example 3.5.5 - Output

Pi is approximately       3.142

Example 3.5.5 - Explanation

  • Three decimal places: {PI:.3f} would format PI to 3.142.
  • No decimal places: {PI:.0f} would format PI to 3.
  • Width and alignment: You can also specify a minimum width and alignment as displayed in this example.

 


 

F-strings can also be used to format date and time objects and they are often more readable than other string formatting methods. F-strings are generally faster than other string formatting methods because they are evaluated at runtime and do not involve function calls. Hence you should opt for f-strings formatting when possible.

 


 

Exercise 3.5.1 - Write a Python program that asks for the user's name and age. Print a greeting message that says:

"Hello, [name]! You are [age] years old."

Exercise 3.5.1 - Model Answer - Make sure to work out the exercise before checking the model answer.

 


 

Exercise 3.5.2 - Write another Python program to ask the user to input two numbers. Then print the sum, difference, product, and quotient of these two numbers using f-strings. Finally format the output to show only 2 decimal places for the quotient.

Exercise 3.5.2 - Model Answer - Make sure to work out the exercise before checking the model answer.