f-string
A Python string with an f before the opening quote, in which expressions in curly brackets are replaced by their values.
In more detail
f-strings, added in Python 3.6, are the shortest way to format text: any expression can go inside the braces and a format specifier follows a colon, as in f"{price:.2f}". They are evaluated where they are written, so every name used must exist at that point.
Example
name = "Ana"
print(f"Hello, {name}!")Related terms
- SQL injection — An attack where untrusted input is joined into a database query so that part of it runs as SQL, letting an attacker read or change data.
- Python — A popular general-purpose programming language known for readable code and a large standard library.
- assertion message — Your own text added after an assert, following a comma, that pytest prints when the assertion fails.
- format method — The string method that returns a copy of a string with each {} replacement field filled in from its arguments.
- replacement field — A pair of braces {} in a format string marking where a value will be inserted.
- format specifier — The instructions after a colon inside a replacement field, such as .2f for two decimal places, that control how a value is shown.
Taught in
- Static Application Security Testing with Semgrep — Cloud Security & DevSecOps Engineering
- The String Format Method — Hands-on Python: Complete Video Course & Book
- Dictionaries And String Formatting — Hands-on Python: Complete Video Course & Book
- Making Failures Explain Themselves — Automated Testing, TDD & Quality Engineering