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:
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.
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.
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.
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.
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.
In this example, the expression x + y * 2
is evaluated as x + (y * 2)
, following the standard order of operations in Python.
Quiz Question
Hint: Consider how f-strings work in Python and how they handle expressions inside the curly braces. What value will be substituted for {age + 5}
when the string is formatted?
The correct answer is B) Hello, my name is John and I am 30 years old.
Quiz Question
Hint: Consider how formatting specifications work in f-strings for specifying the number of decimal places. Which option correctly formats the floating-point number pi
to have exactly two decimal places?
The correct answer is A) f"Pi value is {pi:.2f}"
.
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.