More Flexibility in Python F-String
Python 3.12 lifts some restrictions on f-strings, allowing for more flexibility in their usage.
Quote Reuse: F-strings now support reusing the same quotes as the enclosing f-string, enabling nested f-strings.
songs = ['Take me back to Eden', 'Alkaline', 'Ascensionism']
f"This is the playlist: {', '.join(songs)}"
Multi-line Expressions and Comments: F-strings can now span multiple lines and include inline comments.
f"This is the playlist: {', '.join([
'Take me back to Eden', # My, my, those eyes like fire
'Alkaline', # Not acid nor alkaline
'Ascensionism' # Take to the broken skies at last
])}"
Backslashes and Unicode Characters: F-string expressions can now contain backslashes and unicode escape sequences.
print(f"This is the playlist: {'\n'.join(songs)}")
print(f"This is the playlist: {'\N{BLACK HEART SUIT}'.join(songs)}")
Python 3.12 – What’s New and How to Download?
Python releases a new version almost every year. The latest version, that is, Python 3.12 was released on 2 October, 2023. This version introduced many new features and improvements. In this article, we will see some of the newly added features to Python 3.12.
Table of Content
- Download and Install Python
- Improved Error Messages in Python
- More Flexibility in Python F-String
- Type Parameter Syntax
- Improvement in Modules
- Syntactic Formalization of f-strings
- Per-Interpreter GIL
- Low Impact Monitoring for CPython
- Conclusion