从Python变量中获取多行字符串

时间:2017-01-15 20:55:05

标签: python list listview

我有以下代码来获取每个实例的名称和启动时间:

for instance in instances:

    instance_name = instance.name
    launch_time = instance.launch_time

结果我想得到一个带有列表的变量:

instance_name: launch_time
instance_name: launch_time
...

Name: server1; Launch time: 2 days, 7:33:46.319073
Name: server2: Launch time: 4 days, 6:33:46.319073
...

然后将其用于另一个多行字符串中的电子邮件通知。 我已经完成了:

result = []
result.append({'name':instance_name, 'launch_uptime':uptime })

但我不喜欢这些引号,我想要一个好的格式化文本

1 个答案:

答案 0 :(得分:1)

你可以这样做:

multi_line = ''

for instance in instances:
    instance_name = instance.name
    launch_time = instance.launch_time

    multi_line += 'Name: ' + instance_name +'; Launch time: ' + launch_time + '\n'

print(multi_line)