I was wondering if we can print like row-wise in python.
Basically I have a loop which might go on million times and I am printing out some strategic counts in that loop.. so it would be really cool if I can print like row-wise
print x
# currently gives
# 3
# 4
#.. and so on
and i am looking something like
print x
# 3 4
unutbu
796k170 gold badges1720 silver badges1622 bronze badges
asked Dec 8, 2011 at 21:31
1
In Python2:
data = [3, 4]
for x in data:
print x, # notice the comma at the end of the line
or in Python3:
for x in data:
print[x, end=' ']
prints
3 4
answered Dec 8, 2011 at 21:34
unutbuunutbu
796k170 gold badges1720 silver badges1622 bronze badges
0
Just add a ,
at the end of the item[s] you're printing.
print[x,]
# 3 4
Or in Python 2:
print x,
# 3 4
answered Dec 8, 2011 at 21:34
AphexAphex
7,2605 gold badges33 silver badges53 bronze badges
3
You don't need to use a for loop to do that!
mylist = list['abcdefg']
print[*mylist, sep=' ']
# Output:
# a b c d e f g
Here I'm using the unpack operator for iterators: *
. At the background the print function is beeing called like this: print['a', 'b', 'c', 'd', 'e', 'f', 'g', sep=' ']
.
Also if you change the value in sep
parameter you can customize the way you want to print, for exemple:
print[*mylist, sep='\n']
# Output:
# a
# b
# c
# d
# e
# f
# g
answered Aug 14, 2020 at 19:21
If you add comma at the end it should work for you.
>>> def test[]:
... print 1,
... print 2,
...
>>> test[]
1 2
answered Dec 8, 2011 at 21:34
1
my_list = ['keyboard', 'mouse', 'led', 'monitor', 'headphones', 'dvd']
for i in xrange[0, len[my_list], 4]:
print '\t'.join[my_list[i:i+4]]
answered Dec 8, 2011 at 21:35
Zain KhanZain Khan
3,7122 gold badges31 silver badges50 bronze badges
a=int[input["RangeFinal "]]
print["Prime Numbers in the range"]
for n in range[2, a]:
p=0
for x in range[2, n]:
if n % x == 0:
break
else:
if[p==0]:
print[n,end=' ']
p=1
Answer
RangeFinal 19
Prime Numbers in the range
3 5 7 9 11 13 15 17
answered May 25, 2012 at 7:08
Use this code for your print
print[x,end=""]
answered Mar 6, 2018 at 7:59
Mr NobodyMr Nobody
3773 silver badges4 bronze badges
For python 2:
for x in num:
print x,
For python 3:
for x in num:
print[x, end = ' ']
answered Jun 25, 2018 at 8:24
Python 3:
l = [3.14, 'string', ['tuple', 'of', 'items']]
print[', '.join[map[repr, l]]]
Output:
3.14, 'string', ['tuple', 'of', 'items']
answered Apr 25, 2019 at 9:41
Tom HaleTom Hale
35.2k27 gold badges163 silver badges224 bronze badges