简体   繁体   中英

Get multiline string from Python variables

I have the following piece of code to get name and launch time for each instance:

for instance in instances:

    instance_name = instance.name
    launch_time = instance.launch_time

As result i want to get a variable with the list like:

instance_name: launch_time
instance_name: launch_time
...

ie

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

and then use it for email notification in another multi-line string. I've done it with:

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

but i don't like these quotes, i want a good formatted text

You can do like this:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM