from cryptography.fernet import Fernet
data = "Here is some data I would like to encode!".encode('utf-8')
my_key = Fernet.generate_key() # keep this key safe! DO NOT LOSE IT!
# If you lose your key, any data that you encrypted and didn't decrypt
# will be lost forever. (Unless, of course, you have an excellent memory
# and can remember a 32 character long series of random and
# cryptographically secure bytes. In which case, you're fine.)
t = Fernet(my_key)
encrypted = t.encrypt(data)
# This will return a series of bytes, so to convert it back to a string:
boring_old_string = encrypted.decode()
# If you wanted to decrypt a message:
decrypted = t.decrypt(encrypted)
# This will return bytes, so use the method above to make it a string