A First Look at Python 3.6
An early look at the exciting new features coming in Python 3.6. We explore the new f-strings for improved string formatting, underscores in numeric literals, and the new dictionary implementation.
As the Python community gears up for the final release of Python 3.6 later this year, the beta releases have given us a clear look at the new features we can expect. And there's a lot to be excited about. This release is packed with features that will improve developer productivity and code readability.
Let's take a look at some of the most anticipated new features in Python 3.6.
The Future of String Formatting: f-strings
This is the headline feature, and it's a big one. Python 3.6 introduces a new way to format strings called formatted string literals, or f-strings. They promise to be more readable, more concise, and faster than the existing methods (%
formatting and str.format()
).
To create an f-string, you simply prefix a string literal with f
and then write your expressions directly inside curly braces {}
within the string.
name = "Alice"
age = 30
# The new f-string syntax
message = f"My name is {name} and I am {age} years old."
This is a huge improvement in readability compared to str.format()
, where you have to look back and forth between the string and the arguments to the format
method. With f-strings, the expression is right there where it's being used.
Underscores in Numeric Literals
This is a small but fantastic quality-of-life improvement for readability. You will now be able to use underscores in numbers to act as visual separators for digits.
# This will be valid in Python 3.6
one_billion = 1_000_000_000
# It also works for hex and binary numbers
hex_address = 0xFF_EC_DE_5E
The underscores are ignored by the interpreter but make it much easier for developers to read and reason about large numbers.
A New, Faster, More Compact Dictionary
The implementation of the dict
type has been completely rewritten for CPython 3.6. The new implementation is more memory-efficient and faster. As a side effect of this new implementation, dictionaries will now preserve their insertion order. This means that when you iterate over a dictionary, the items will appear in the same order you inserted them. While the developers have stated that this is an implementation detail and shouldn't be relied upon (yet!), it's a very welcome side effect that may become an official language feature in the future.
Asynchronous Generators
Building on the async
/await
syntax introduced in Python 3.5, version 3.6 adds support for asynchronous generators. This will allow for the creation of asynchronous data streams, which is a powerful concept for I/O-bound applications.
Conclusion
Python 3.6 is shaping up to be a release packed with features that will make Python developers happier and more productive. f-strings, in particular, are a game-changing addition that will likely become the standard way to format strings in all modern Python code. The focus on readability and developer ergonomics shows that the language continues to evolve in a thoughtful and practical way.