3 features you don’t wanna miss in Python 3.9

Carlos Equiz
nomadglobal-engineering
2 min readJun 16, 2020
Photo by Simon Abrams on Unsplash

Since version 3.9.0, Python adopted a release calendar with annual cadence making releases smaller. The final planned release for Python 3.9.0 is scheduled for October 2020.

Let’s review the new handy features for string and dictionary manipulation and the improved typing module.

1. Dictionary Merge & Update Operators

Merging two dictionaries was never easier with union operators

# Merge two dictionaries with | operatordogs = { 'shiba_inu': 10, 'puddle': 4}
cats = { 'persian': 2, 'stray': 10 }
cats_and_dogs = dogs | cats
print(cats_and_dogs)
[Out]: {'shiba_inu': 10, 'puddle': 4, 'persian': 2, 'stray': 10}# Update a dictionary with |= operatoranimals = { 'shiba_inu': 10, 'puddle': 4, 'parrots': 6}
cats = { 'persian': 2, 'stray': 10 }
animals |= cats
print(animals)
[Out]: {'shiba_inu': 10, 'puddle': 4, 'parrots': 6, 'persian': 2, 'stray': 10}# Dictionaries with common key, the last key-value will be preservedanimals = { 'shiba_inu': 2, 'puddle': 4, 'persian': 6}
cats = { 'persian': 2, 'stray': 10 }
animals |= cats
print(animals)
[Out]: {'shiba_inu': 2, 'puddle': 4, 'persian': 2, 'stray': 10}

2. String methods: removeprefix() and removesuffix()

Fast and clean removal of prefix or suffix from a string with removeprefix() and removesuffix()

customer_id = "id_056a8ecd-be09-4a92-984b-d5045960b495_customer"print(customer_id.removeprefix("id_"))[Out]: "056a8ecd-be09-4a92-984b-d5045960b495_customer"print(customer_id.removesuffix("_customer"))[Out]: "id_056a8ecd-be09-4a92-984b-d5045960b495"print(customer_id.removeprefix("id_").removesuffix("_customer")))[Out]: "056a8ecd-be09-4a92-984b-d5045960b495"

3. Type Hinting

Perhaps one of the most interesting improvements in this version has to do with the typing module, by introducing a simplified and annotated way to define type-hint metadata.

Boost your productivity and enhance your developer experience with hinting types with static analysis or at runtime right in your code editor. 🚀

def sum_int(value: int):
return value + value
sum_int("weird value"):# Your code editor will hint the following:
Expected type 'int', got 'str' instead

Final Words

Although Python 3.9.0 is not yet ready for production, you can experiment with it by downloading and installing the 3.9.0b3 beta release.

Happy coding!

--

--