Python Forum

Full Version: Python-for loop print into single line
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Code bellow prints each line for multiple output. Is it possible to print this output into single line:

   reservations = ec.describe_instances().get('Reservations', []) 

   for reservation in reservations:
        for instance in reservation['Instances']:
            tags = {}
            for tag in instance['Tags']:
                tags[tag['Key']] = tag['Value']
                if tag['Key'] == 'Name':
                    name=tag['Value']

            if not 'Owner' in tags or tags['Owner']=='unknown' or tags['Owner']=='Unknown':
                 result=""
                 result.join(name)
Current Output:

Output:
aws-opsworks Ansible
Desired output:

Output:
aws-opsworks Ansible
I don't see any print function in your code.
Anyway, 'print' has an optional argument 'end' which default value is the new line character. This is why when you print something the function adds a new line at the end. Change it to an empty string - print(text, end=""). Or space.
Already tried it and it works, however, i need to export print output to some variable, so i can pass that variable to function for sending email

so i tried
body=str(print(name,""))
, but then getting
None
as output
The print function returns None, so you return the name variable
Just pass the name variable to the function.