Control flow and looping, printing to console on one line? 3/13


#1

Is there a way to print to the console where it prints to a single line on the console instead of a line for each item?

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

for numbers in a:
    if numbers % 2 == 0:
        print numbers #That prints the items to a different line on the console.

I don't want it to print with [ ] to the console just the items in the list on one line?

so it should look like: 0 2 4 6 8 10 12
Instead of:
0
2
4
6
8
10
12


#3

How will that look can you please explain and thanks in advance?


#4

try this

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

for numbers in a:
    if numbers % 2 == 0:
        print numbers,
        #That prints the items to a different line on the console.

Sorry initially i thought it was javascript
in python 2.7 if you want to print element on one line you do print (element,)
in python 3.x if you want to print element on one line you do print (element, end="")


#5

Thank you again. You know that was bugging me not knowing and it was a simple comma or comma end=""


#6

yeah python sometimes has these hideous shortcuts


#7

I have another question regarding this how do you go to another statement after it? It seems to put the next one in that same line.


#8

can you elaborate on your question


#9
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

for numbers in a:
    if numbers % 2 == 0:
        print numbers,

That prints them on one line. Now say I want to do something else and then print that something else. It goes on that line also. How do I go to the next line printing to console?


#10

you could do something like this

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

for numbers in a:
    if numbers % 2 == 0:
        print numbers,
        
print("\nme")

use the "\n" (new line) character or you can just do print(python2.7) or print()(python3.x)


#11

That's exactly what I was thinking and I just deleted "me" for it to be blank. Thanks!