使用列表的所有元素格式化字符串 [英] Format string with all elements of a list

查看:288
本文介绍了使用列表的所有元素格式化字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

words = ['John', 'nice', 'skateboarding']
statement = "%s you are so %s at %s" % w for w in words

产生

File "<stdin>", line 1
statement = "%s you are so %s at %s" % w for w in words
                                           ^
SyntaxError: invalid syntax

我在这里做错了什么?假设是:len(words)==语句中'%s'的数量

What am I doing wrong here? The assumption is: len(words) == number of '%s' in statement

推荐答案

>>> statement = "%s you are so %s at %s" % tuple(words)
'John you are so nice at skateboarding'

这篇关于使用列表的所有元素格式化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆