Formatted String Literals, often referred to as f-strings, are a feature introduced in Python 3.6 and later versions that allow you to embed expressions inside string literals using curly braces ({}). F-strings provide a concise and readable way to create formatted strings, making it easier to include variables, expressions, and other values in your strings. Here's everything you need to know about f-strings:

Syntax of f-strings

F-strings are created by prefixing a string literal with the letter f or F. Inside an f-string, expressions are enclosed in curly braces ({}) and are evaluated at runtime. The general syntax of an f-string is:

f"string text {expression}"

Embedding expressions

You can embed variables, constants, or expressions inside f-strings using curly braces ({}). The expressions inside curly braces are evaluated, and their values are inserted into the string at that location. This makes it straightforward to include dynamic content in your strings.

Example:
name = "Alice"
age = 30
print(f"My name is {name} and I am {age} years old.")
Output:
My name is Alice and I am 30 years old.

Formatting options

F-strings support various formatting options to control the appearance of values, such as specifying the number of decimal places for floating-point numbers. You can include format specifiers inside the curly braces to format the inserted values.

Example:
price = 19.95
print(f"The price is ${price:.2f}")
Output:
The price is $19.95

In this example, .2f specifies that the floating-point number price should be formatted with two decimal places.

Expressions in curly braces

Inside the curly braces ({}), you can include Python expressions, including function calls, mathematical operations, and more. This flexibility allows you to perform calculations and manipulate values directly within the string.

Example:
x = 5
y = 3
print(f"The sum of {x} and {y} is {x + y}.")
Output:
The sum of 5 and 3 is 8.

Escape characters and special characters

F-strings support escape characters and special characters just like regular string literals. You can include newline characters \n, tabs \t, and other escape sequences within f-strings to format your text appropriately.

Example:
print(f"First Line\nSecond Line")
Output:
First Line
Second Line

Expression evaluation order

Expressions inside f-strings are evaluated from left to right, and their results are concatenated into the string. You can use parentheses to control the order of evaluation and ensure that complex expressions are computed correctly.

Example:
x = 2
y = 3
print(f"The result is {x + y * 2}")
Output:
The result is 8

In this example, the expression x + y * 2 is evaluated as x + (y * 2), following the standard order of operations in Python.

Quiz Question

What will the following Python code using an f-string print to the console?

name = "John"
age = 25
print(f"Hello, my name is {name} and I am {age + 5} years old.")

Quiz Question

Which f-string expression will correctly format a floating-point number pi with exactly two decimal places?

Considering these points…

F-strings provide a powerful and convenient way to create formatted strings in Python, making your code more readable and expressive. They are widely used for tasks such as generating log messages, formatting output, and constructing dynamic strings in a concise manner. By mastering f-strings, you can enhance the clarity and functionality of your Python code, making it easier to work with and understand.