我是新手,学习如何在字符串中使用字符串插值,并且我很难得到这个例子,我正在努力实际打印正确的结果.
我试过这样做:
print "My name is {name} and my email is {email}".format(dict(name="Jeff", email="me@mail.com"))
它错误地说 KeyError: 'name'
然后我尝试使用:
print "My name is {0} and my email is {0}".format(dict(name="Jeff", email="me@mail.com"))
它打印出来
My name is {'email': 'me@mail.com', 'name': 'Jeff'} and my email is {'email': 'me@mail.com', 'name': 'Jeff'}
所以我试着这样做:
print "My name is {0} and my email is {1}".format(dict(name="Jeff", email="me@mail.com"))
它错误地说 IndexError: tuple index out of range
它应该给我以下输出结果:
My name is Jeff and my email is me@mail.com
谢谢.
只需删除以下呼叫dict
:
>>> print "My name is {name} and my email is {email}".format(name="Jeff", email="me@mail.com") My name is Jeff and my email is me@mail.com >>>
以下是字符串格式的语法参考.