How can i do this format with python 3.6 F-String ?
person = {'name': 'Jenne', 'age': 23} print('My name {0[name]} and my age {1[age]}'.format(person, person)) print('My name {0} and my age {1}'.format(person['name'], person['age']))
As of Python 3.6, f-strings are a great new way to format strings. Not only are they more readable, more concise, and less prone to error than other ways of formatting, but they are also faster!
Strings in Python are usually enclosed within double quotes ( "" ) or single quotes ( '' ). To create f-strings, you only need to add an f or an F before the opening quotes of your string. For example, "This" is a string whereas f"This" is an f-String.
The %d formatter is used to input decimal values, or whole numbers. If you provide a float value, it will convert it to a whole number, by truncating the values after the decimal point. The %f formatter is used to input float values, or numbers with values after the decimal place.
Well, a quote for the dictionary key is needed.
f'My name {person["name"]} and my age {person["age"]}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With