如何将列表中的所有项添加到变量中?

时间:2015-08-22 14:43:23

标签: python

我是Python的初学者,遇到了一个我无法解决的问题。我有一个名为body的变量,我将其传递给函数以发送电子邮件。然后我有一个名为items的列表,我想把它放在邮件正文中。

我的代码如下所示:

body = "The following items are in the list:"

如何将items列表中的所有项目附加到body变量中字符串的末尾?

4 个答案:

答案 0 :(得分:0)

这样的事情应该做:

SQL> grant imp_full_database  to expimp_user;
grant imp_full_database  to expimp_user
*
ERROR at line 1:
ORA-00604: error occurred at recursive SQL level 1
ORA-20997: "IMP_FULL_DATABASE" grants not allowed
ORA-06512: at "RDSADMIN.RDSADMIN", line 51
ORA-06512: at line 2

答案 1 :(得分:0)

lst = ['first', 'second']
body = "The following items are in the list:"

print(body + ' ' + ' '.join(lst))

输出:

The following items are in the list: first second

some_string.join(list)返回在所有元素之间添加some_string的字符串。

答案 2 :(得分:0)

您可以使用python的字符串格式来完成此任务:

body = "The following items are in the list:"
items = ["first", "second", "third"]
body = "{} {}.".format(body, ' '.join(items))

将返回

'The following items are in the list: first second third.'

答案 3 :(得分:0)

# choose a delimiter for items in the list
delimiter = ' '

# join each item in the list separated by the delimiter
items_str = delimiter.join(str(i) for i in items)

body = "The following items are in the list: {}".format(item_str)