Python 3.10 tips

Handy tips and notes about Python 30.8 10, 11 and beyond.

F strings

Python 3.8 brings PEP489 formatted strings or “f-strings” which make string interpolation easier and readable:

foo = 'bar'
fstring = f"F strings are cool {foo}"
print(fstring)

# Output:
F strings are cool bar

If you do not have a debugger handy you can use f string to print variable name and its value with little effort.

foo = 'bar'
fstring = f"{foo=}"
print(fstring)

# Output:
foo=bar

It even supports expressions:

foo = 'bar'
fstring = f"Foo lenght: {len(foo)+1}"
print(fstring)

# Output:
Foo lenght: 4

Install Python 3.10 on Ubuntu 20.04

sudo apt update && sudo apt upgrade -y
# this lets us add the ppa
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:deadsnakes/ppa
# Install python 3.10
sudo apt install python3.10
# Install venv
sudo apt-get install python3.10-venv

If you do not install python3.10-venv you will get this error when trying to create a venv and the activate scripts will be missing:

$ python -m venv ./venv
Error: Command '['/home/brent/venv/bin/python3.10', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.

Comments

comments powered by Disqus