The improved f-strings in Python 3.12

How they can make your code simpler yet more powerful

Anna Astori
3 min readOct 9, 2024
Photo by Michael Skok on Unsplash

With Python 3.13 release scheduled for early October 2024, I retrospect on one of the Python 3.12 enhancements that actively, I probably use the most in my day-to-day work as I deal a lot with data processing.

Python 3.12 brought a few great updates, and among them are several significant improvements to f-strings, Python’s popular string formatting feature. Introduced in Python 3.6, f-strings (formatted string literals) have become a popular option for embedding expressions in strings, offering a concise and readable alternative to older string formatting methods like % formatting or str.format().

F-strings have become particularly powerful and flexible since Python 3.12, in part thanks to the switch over to the PEG parser in Python 3.9. In this post, I’ll go over the key updates and show how these enhancements can improve your code — making it cleaner and more expressive.

  1. Re-using quotes

You can re-use the same type of quotes as the ones that are used inside the data structure that you are pulling your variable from in the f-string:

user = {
"name": "Alice",
"is_admin": False,
"email": "alice@example.com"
}

print(f"User: {user["name"]}")

Output:

User: Alice

2. Backslash for newlines and unicode characters

You can use backslashes to include new lines to format the output and make it more readable if needed:

to_do_list = ["running", "laundry", "groceries"]
print(f"- {"\n- ".join(to_do_list)}")

Output:

- running
- laundry
- groceries

This also means you can use various Unicode escape sequences, e.g.:

print(f"\nOutput: \N{GRINNING FACE}\n")

In the command line, we will see:

Grinning Face Emoji

3. Multi-line expressions and comments

With Python 3.12, list elements don’t have to be all on one line and they can even contain comments:

print(f"- {"\n- ".join(["running", # This should be done first
"laundry",
"groceries"])}")

Output:

- running
- laundry
- groceries

4. More precise error messages

F-string expressions can sometimes fail due to syntax errors or invalid expressions inside the curly braces. In Python 3.12, error messages for f-strings have been updated and sometimes provide clearer and more helpful messages, showing where the issue is located within the f-string, for example:

count = 1
print(f"Count: {count.}")

In older versions of Python, you would get a generic error message that could be a little confusing. In Python 3.12, the error message is much clearer, pointing out that the syntax after count is incorrect:

Error Message

This improvement makes debugging f-strings much easier, especially in complex formatting situations.

The f-string updates in Python 3.12 brought several important enhancements, making this feature even more robust and flexible. With better error messages and support for more complex expressions, f-strings are now more convenient than ever for Python developers.

These updates not only improve code readability but also make debugging and formatting tasks easier to manage. Whether you’re generating dynamic strings, logging output, or formatting numerical data, the improvements in Python 3.12 give you more control and power with less effort.

If you’re interested in Python, check out my previous posts on optimizing Python code and function decorators in Python.

--

--

Anna Astori

Software Engineer. Ex-Decathlon, ex-Amazon (Alexa AI). I’m also a big figure skating fan and a foodie.