Reputation: 25
I am trying to print a line of code but there's a lot of it and I think it would look neater if I printed it all on one line. I am trying to print a list with a for loop and I would like to print it all on the same line.
for i in ALLROOMS:
print(i.name)
Upvotes: 1
Views: 9838
Reputation: 250881
Use end=" "
:
print (i.name, end=" ")
example:
In [2]: for i in range(5):
...: print(i, end=" ")
...:
0 1 2 3 4
help on print()
:
print(value, ..., sep=' ', end='\n', file=sys.stdout)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
Upvotes: 4
Reputation: 1392
do you mean:
print "|".join(str(v) for v in L) # => 1|2|3
#still can add condition
print "|".join(str(v) for v in L if v>0) # =>1|2|3
of course, you can replace "|" to any character you like.
if all items in the list are string, you can just
print "".join(L)
Upvotes: 3
Reputation: 142106
You may also want to consider the pprint module module:
from pprint import pprint
pprint(i.name)
It won't necessarily print on the same line, but it's customisable as to width and such - and is generally a nice way of producing "more readable" outputs.
Upvotes: 1