Try this:
print(j, end='')
end
by default is \n
(see print()
). Also, be sure to print a newline at the end of each outer loop iteration:
for i in range(1,6): # notice that I changed this to 6
for j in range(1,i):
print(j, end='') # added end=''
print() # printing newline here
1
12
123
1234
EDIT I just noticed you were using Python 2.7. Since that's the case, you can use print j,
instead of print(j, end='')
and print
instead of print()
. Note that print j,
will leave spaces between the j
s. If you don't want this, you can import sys
and use sys.stdout.write(j)
instead (see sys
).
Furthermore, if you want to use the Python 3 print function as shown above, you can always
from __future__ import print_function