What are Python f-strings?
With Python f-strings, you can insert complex expressions and variables directly into a string, without having to perform additional conversions or formatting.
What can Python f-strings do?
Python f-strings are a Python string format that were introduced with Python 3.6. They’re also known as formatted string literals. Within an f-string, you can evaluate calculations using curly brackets. F-strings allow more compact notation than other string formats like str.format()
and concatenation with +
. This ultimately leaves you with clearer and more concise code.
F-strings are highly flexible and allow you to insert various data types like integers, floats, lists, expressions and functions without converting them or adding special formatting. Python f-strings tend to be faster than other string formatting methods in Python.
What is the syntax of Python f-strings?
The basic structure of a Python f-string consists of the prefix f
or F
, followed by a string in quotation marks (“ or ’). Within the string, you can use curly brackets {}
to embed variables and expressions.
In this example, we put the variables name
and age
in curly brackets in the f-string –{name}
and {age}
.
- 99.9% uptime
- PHP 8.3 with JIT compiler
- SSL, DDoS protection, and backups
How are Python f-strings used?
F-strings can be used in a variety of ways.
Performing calculations within a string
Python f-strings are especially practical if you want to do arithmetic calculations within a string. They allow you to define a complex expression in a single line of code.
In this example, num1
and num2
are added within the f-string. The result is shown in the final string output.
Python f-strings and raw strings
The combination of r
for raw strings and f
for f-strings creates a special kind of string in Python, known as an rf-string. With rf-strings, you can combine the functionality of raw strings with the flexibility of f-strings. R-strings interpret escape sequences literallyand f-strings can directly embed variables and expressions in a string.
In the above code, we used an rf-string to define a path. We used {name}
in the rf-string to directly insert the variable name
. Meanwhile, the r
before the string ensures that the backslash \
is treated as a literal character and not part of an escape sequence.
Precision of floating numbers
You can use special formatting statements to specify the number of decimal places in a floating number in an f-string.
In the above code, the formatting statement :.3f
indicates that the variable value
should be rounded to three decimal places and inserted into the formatted string.
Width and alignment
In Python f-strings, width and alignment specifications allow you to control how values are displayed within a field. This is useful for placing text and numbers in a specific place and aligning them left, right or center.
Say we have the name __Alice__
and want to right-align it in a field that’s 10 characters in width.
Since the value is less than 10 characters long, {name:>10}
will right-align it.
Filling with zeros or other characters
Filling with zeros or other characters influences the alignment of numbers in a field. This is useful if you want numbers in a fixed format, such as when you’re displaying times or numerical values.
In this example, :06
indicates that the number for number
will be entered in a six-digit field and any missing digits will be filled in with leading zeros.
You can also use characters other than zeros to fill a string: