Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do a dictionary format with f-string in python 3.6?

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'])) 
like image 790
Abou Menah Avatar asked Apr 19 '17 06:04

Abou Menah


People also ask

Does Python 3.6 support F strings?

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!

How do you format F in Python?

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.

How do you use %d and %f in Python?

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.


1 Answers

Well, a quote for the dictionary key is needed.

f'My name {person["name"]} and my age {person["age"]}'

like image 115
M. Leung Avatar answered Sep 20 '22 23:09

M. Leung