Python Forum
Python-for loop print into single line
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python-for loop print into single line
#1
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
Reply
#2
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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
Reply
#4
The print function returns None, so you return the name variable
Recommended Tutorials:
Reply
#5
Just pass the name variable to the function.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with spliting line in print akbarza 3 335 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,576 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  python sql query single quote in a string mg24 1 996 Nov-18-2022, 08:01 PM
Last Post: deanhystad
  Print the line before the corrent line tester_V 9 1,489 Nov-18-2022, 08:39 AM
Last Post: Gribouillis
  python Multithreading on single file mg24 3 1,672 Nov-05-2022, 01:33 PM
Last Post: snippsat
  Skipping line in text without Restarting Loop IdMineThat 4 1,433 Apr-05-2022, 04:23 AM
Last Post: deanhystad
  Print to a New Line when Appending File DaveG 0 1,190 Mar-30-2022, 04:14 AM
Last Post: DaveG
  How to prevent python from going to new line in for loop? idknuttin 3 4,841 Feb-11-2022, 05:40 AM
Last Post: deanhystad
  How to make for loop display on 1 Line Extra 3 1,377 Jan-12-2022, 09:29 PM
Last Post: Extra
  multi-line CMD in one-line python kucingkembar 5 3,862 Jan-01-2022, 12:45 PM
Last Post: kucingkembar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020