New Features in Python 3.8 and 3.9
Python 3.8 and 3.9 comes with some useful features. Some are listed here
- Merging Dictionaries
The old style of merging Python dictionaries is using ** eg {**d1, **d2} which is not intuitively. The new way of merging dictionaries is using | as shown the in the example of new dictionaries merging code below:
import cv2 as cv
a = {'France':'Paris','Thailand':'Bangkok'}
b = {'Germany':'Berlin','UK':'London'}
print(a|b)
Output:{‘France’: ‘Paris’, ‘Thailand’: ‘Bangkok’, ‘Germany’: ‘Berlin’, ‘UK’: ‘London’}
2. f-Strings Literal
f-strings, also called “formatted string literal”, ere string literals that have an f
at the beginning and curly braces containing expressions that will be replaced with their values. They replace the old clumsy style %-format and str-format syntax. See the example of using f-strings formatting below:
country = 'France'
capital = 'Paris'
print(f'The capital of {country} is {capital}')
Output:The capital of France is Paris
References:
- Python 3’s f-Strings: An Improved String Formatting Syntax (Guide)
- What’s New In Python 3.9
- What’s New In Python 3.8
Relevant Courses
April 15, 2021