python3.6:使用以下格式的%d%s的等价物:f(“string {var1} {var2})

时间:2017-10-02 14:07:22

标签: python python-3.x syntax

查看print的语法:

for index, link in enumerate(links):
   args = (index, link.xpath('@href').extract(), 
   link.xpath('img/@src').extract())
   print ('Link number %d points to url %s and image %s' % args)

此代码适用于python 3.6。它发送:

Link number 0 points to url [u'image1.html'] and image [u'image1_thumb.jpg']

但使用f“...... {var}”是否存在等价物?

看起来这个不起作用:

print (f'Link number {repr(enumerate)} points to url {str(index)} and image {str(link)}')

发送:

Link number 0 points to url <Selector xpath='//a[contains(@href, "image")]' data='<a href
="image1.html">Name: My image 1 <'>

您可以在此troubleshooting article tuto中找到完整代码。

1 个答案:

答案 0 :(得分:3)

感谢@JonClements,以下是答案:

print('Link number {} points to url {} and image {}'.format(*args))