Python 如何让打印声明告诉我作者的日期';死亡?

Python 如何让打印声明告诉我作者的日期';死亡?,python,python-3.x,loops,dictionary,Python,Python 3.x,Loops,Dictionary,我试图让它说“作者”死于“日期”,但它不起作用 authors = { "Charles Dickens": "1870", "William Thackeray": "1863", "Anthony Trollope": "1882", "Gerard Manley Hopkins": "1889" } for author, date in authors.items(): print( "%s" % authors + " died in " + da

我试图让它说“作者”死于“日期”,但它不起作用

authors = {
   "Charles Dickens": "1870",
   "William Thackeray": "1863",
   "Anthony Trollope": "1882",
   "Gerard Manley Hopkins": "1889"
   }
for author, date in authors.items():
   print( "%s" % authors + " died in "  + date)

您只需将打印中变量的名称从authors(您的完整dict)更改为author,表示集合的每个实例:

authors = {
   "Charles Dickens": "1870",
   "William Thackeray": "1863",
   "Anthony Trollope": "1882",
   "Gerard Manley Hopkins": "1889"
   }
for author, date in authors.items():
    print( "%s" % author + " died in "  + date)
这将产生以下输出:

Charles Dickens died in 1870
William Thackeray died in 1863
Anthony Trollope died in 1882
Gerard Manley Hopkins died in 1889

您可以使用该格式

authors = {
   "Charles Dickens": "1870",
   "William Thackeray": "1863",
   "Anthony Trollope": "1882",
   "Gerard Manley Hopkins": "1889"
   }
for author, date in authors.items():
     print('{}, in {}'.format(author, date)))
或f-string Python 3.6及以上版本

for author, date in authors.items():
   print('{author}, in {date}'))
输出

Charles Dickens, in 1870
William Thackeray, in 1863
Anthony Trollope, in 1882
Gerard Manley Hopkins, in 1889

以下是简单易懂的解决方案

for author, date in authors.items():
   print( author + " died in "  + date)
输出

Charles Dickens died in 1870
William Thackeray died in 1863
Anthony Trollope died in 1882
Gerard Manley Hopkins died in 1889

您需要将
print
语句中的
authors
更改为
author
, 您可以使用多种打印样式,明智地选择其中一种

authors = {
   "Charles Dickens": "1870",
   "William Thackeray": "1863",
   "Anthony Trollope": "1882",
   "Gerard Manley Hopkins": "1889"
   }
for author, date in authors.items():
   #your statement
   print( "%s" % author + " died in "  + date)

   #using f before the string for python 3.6 and above
   print(f"{author} died in {date}")


   #using format() method of string
   print("{0} died in {1}".format(author,date))

   #using string concatenation
   print(author + " died in "+date)
每个语句都打印相同的输出,但有些技术比其他技术更有用,例如使用
f
format()
可以为您提供更大的灵活性


希望这对你有帮助

查尔斯·狄更斯,1870年威廉·萨克雷,1863年安东尼·特罗洛普,1882年杰拉德·曼利·霍普金斯,1889年这就是不断出现的{“查尔斯·狄更斯:'1870”,“威廉·萨克雷:'1863”,“安东尼·特罗洛普:'1882”,“杰拉德·曼利·霍普金斯:'1889}死于1870年{'Charles Dickens':'1870','William Thackray':'1863','Anthony Trollope':'1882','Gerard Manley Hopkins':'1889'}死于1863{'Charles Dickens':'1870','William Thackray':'1863','Anthony Trollope':'1882','Gerard Manley Hopkins':'1889'}死于1882年{'Charles Dickens':'1870','William Thackray':'1863','Anthony Trollope':'1882','Gerard Manley Hopkins':'1889'}死亡于1889年虽然这可能回答了这个问题,但添加一些额外的信息,解释代码的作用是很有用的。请注意,f-strings仅为python 3.6及以上版本。对于该注释,Tnx的答案与上面的答案相同。有什么不同?我使用了字符串串联
authors = {
   "Charles Dickens": "1870",
   "William Thackeray": "1863",
   "Anthony Trollope": "1882",
   "Gerard Manley Hopkins": "1889"
   }
for author, date in authors.items():
   #your statement
   print( "%s" % author + " died in "  + date)

   #using f before the string for python 3.6 and above
   print(f"{author} died in {date}")


   #using format() method of string
   print("{0} died in {1}".format(author,date))

   #using string concatenation
   print(author + " died in "+date)