I am using Python version 3.2.3. I am trying to print a list of numbers in a row, the print command seems to always printing the numbers one at row.
Example
numbers = [1, 2, 3, 4, 5];
for num in numbers:
print("\t ", num)
output is:
1
2
...
the required output is 1 2 3 4 5
I would appreciate your help. P.s. I searched the web in here and most of the questions were related to Python 2.7.
Use the end
argument to suppress (or replace) the automatic EOL output:
print("\t ", num, end='')
Or, you should probably just use:
print('\t'.join(map(str, [1, 2, 3, 4, 5])))
In addition to the other answers, there's a neat way to do it now that print is a function:
print(*numbers, sep='\t')
Unlike your original code, this won't put a tab before the first number. If you want that extra tab, the easiest way is just to put a blank item before the numbers, so it creates an extra tab separator:
print('', *numbers, sep='\t')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With