Python Forum
Python-for loop print into single line - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python-for loop print into single line (/thread-14264.html)



Python-for loop print into single line - dragan979 - Nov-22-2018

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



RE: Python-for loop print into single line - wavic - Nov-22-2018

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.


RE: Python-for loop print into single line - dragan979 - Nov-22-2018

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


RE: Python-for loop print into single line - metulburr - Nov-22-2018

The print function returns None, so you return the name variable


RE: Python-for loop print into single line - wavic - Nov-23-2018

Just pass the name variable to the function.