Python 3 f-strings
One of my favorite features of Python 3 is f-strings (Formatted String Literals).
Each of these print
statements emits the same output. But which of them is the easiest to write and read?
company_name = 'Beeswax'
location = 'New York City'
print('The company {} is based in {}. {} is a great place to work.'.format(company_name, location, location))
print('The company {company_name} is based in {location}. {location} is a great place to work.'.format(company_name=company_name, location=location))
print(f'The company {company_name} is based in {location}. {location} is a great place to work.')
Even if that’s all they did, I’d love f-strings. But they’re much more powerful than that. (For example, you can dereference objects inline: f"Number of connections: {aerospike_client.connection_count}"
) In fact, you can evaluate arbitrary expressions including function calls within the f-string braces, and all variables in scope can be passed as arguments. See the references below for the details.
References
https://docs.python.org/3.7/reference/lexical_analysis.html#f-strings