如何在python 3.6中用f-string做字典格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43488137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to do a dictionary format with f-string in python 3.6?
提问by Abou Menah
How can i do this format with python 3.6 F-String ?
我如何使用 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']))
回答by M. Leung
Well, a quote for the dictionary key is needed.
嗯,需要引用字典键。
f'My name {person["name"]} and my age {person["age"]}'
f'My name {person["name"]} and my age {person["age"]}'
回答by Mark Langford
Depending on the number of contributions your dictionary makes to a given string you might consider using .format(**dict)
instead to make it more readable, even though it doesn't have the terse elegance of an f string.
根据您的字典对给定字符串的贡献数量,您可能会考虑使用.format(**dict)
它来使其更具可读性,即使它没有 f 字符串的简洁优雅。
>>> person = {'name': 'Jenne', 'age': 23}
>>> print('My name is {name} and my age is {age}.'.format(**person))
My name is Jenne and my age is 23.
Whilst this option is situational, you might like avoiding a snarl up of quotes and double quotes
虽然此选项视情况而定,但您可能希望避免引号和双引号的混乱
回答by Ashwani Singh
Both of the below statement will work on Python 3.6 onward:
以下两个语句都适用于 Python 3.6 以后的版本:
print(f'My name {person["name"]} and my age {person["age"]}')
print(f"My name {person['name']} and my age {person['age']}")
print(f'My name {person["name"]} and my age {person["age"]}')
print(f"My name {person['name']} and my age {person['age']}")
Please mind the single '
and double "
quotes in the above statements as placing them wrong will give syntax error.
请注意上述语句中的单引号'
和双"
引号,因为放置错误会导致语法错误。
回答by Christian K?nig
Edit: confused the old format-function and the new f-stings. Added clarification.
编辑:混淆了旧的格式功能和新的 f-stings。添加了说明。
The string pkuphy posted is correct, you have to use quotes to access the dictionary:
发布的字符串 pkuphy 是正确的,您必须使用引号来访问字典:
f'My name {person["name"]} and my age {person["age"]}'
Your original string works for the str.format()
-function:
您的原始字符串适用于str.format()
-function:
>>> person = {'name': 'Jenne', 'age': 23}
>>> print('My name is {person[name]} and my age is {person[age]}.'.format(person=person))
My name is Jenne and my age is 23.
The first person
references all occurences in the format-string, the second gives the variable to fill in.
第一个person
引用格式字符串中的所有出现,第二个给出要填充的变量。
回答by pkuphy
This will work.
这将起作用。
f'My name {person["name"]} and my age {person["age"]}'
if name
is a property of obj
, f'name is {obj[name]}'
, but for a dict as in this question, you can direct access the key f'name is {person["name"]}'
.
如果name
是的属性obj
,f'name is {obj[name]}'
但对于一个字典作为这个问题,你可以直接访问的关键f'name is {person["name"]}'
。
回答by mariotomo
you have one object alone, which does not help understanding how things work. let's build a more complete example.
你只有一个对象,这无助于理解事物是如何运作的。让我们构建一个更完整的示例。
person0 = {'name': 'Jenne', 'age': 23}
person1 = {'name': 'Jake', 'age': 29}
person2 = {'name': 'John', 'age': 31}
places = ['Naples', 'Marrakech', 'Cape Town']
print('''My name {0[name]} and my age {0[age]},
your name {1[name]} and your age {1[age]},
my favourite place {3[0]}'''.format(person0, person1, person2, places))
you can also access complete objects from the arguments list, like in
您还可以从参数列表中访问完整的对象,例如
print('{2}'.format(person0, person1, person2, places))
or select attributes, even cascading:
或选择属性,甚至级联:
print('{3.__class__.__name__}'.format(person0, person1, person2, places))