2

I'm not able to remove the spacing in a for-loop as numbers are not coming in the same for making the pattern.

My code:

for i in range(1,5):
    for j in range(1,i):
       print(j)

Produces the following result:

1
1
2
1
2
3

But my desired output is:

1
12
123
1234
Georgy
  • 12,464
  • 7
  • 65
  • 73
Abhishek Jain
  • 65
  • 1
  • 2
  • 8

9 Answers9

7

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 js. 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
arshajii
  • 127,459
  • 24
  • 238
  • 287
1

One line solution, if you're interested:

print('\n'.join([''.join(['{}'.format(i) for i in range(1,j)]) for j in range(2,6)]))

1
12
123
1234
xnx
  • 24,509
  • 11
  • 70
  • 109
  • When we are printing it its printing successfully. But when we try to assign to a variable inside a function and try to print the variable by calling the function it getting as below. '1\n1 2\n1 2 3\n1 2 3 4' why it is so? can u help? – sre Nov 07 '19 at 06:55
1
for r in range(1,5):
    for c in range (1,r+1):
        print c,
    print

here, the print without argument causes printing in the next line

0

In order to get all of the numbers on one line, you'll have to use one print statement per line you want. One way you could do this is:

for i in range(1, 5):
    print(''.join([str(n) for i in range(1, i)]))

Keeping the nested for loops you could do:

for i in range(1, 5):
    temp = ''
    for j in range(1, i):
        temp += str(j)
    print(temp)
mr2ert
  • 5,146
  • 1
  • 21
  • 32
0
def pentagon(num):

    j = num
    for i in range(0, num + 1):
        print('{}{}'.format(' ' * j, ' *' * i))
        j -= 1

pentagon(2)

output

 *  
* *
Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44
Shiva
  • 51
  • 6
0

Try:

def RTN():
    x = 1
    num = int(input('Type any integer besides 0: '))
    if num == 0:
        return 'Try again!'
    while x < num + 2:
        print('')
        for y in range(1, x):
            print(str(y) + ' ', end = '')
        x += 1
user36339
  • 243
  • 3
  • 14
0
num = int(input())

for i in range (1,num+1):
    for j in range(i):
        print(j+1,end = '')
    print("")
4b0
  • 21,981
  • 30
  • 95
  • 142
0

I thought the idea of @Shiva was really nice and made a slightly more general pyramid function, maybe someone can use/enjoy it it:

def pyramid(n_rows, s, upside_down=False, offset=0):
    whites = ' ' * len(s)
    offset = ' ' * offset

    indices = np.arange(n_rows)

    if upside_down:
        indices = zip(indices[::-1]+1, indices)
    else:
        indices = zip(indices+1, indices[::-1])

    for i, j  in indices:
        print(f"{offset + whites * j}{(s + whites) * i}")


pyramid(4, 'SO')
#       SO  
#     SO  SO  
#   SO  SO  SO  
# SO  SO  SO  SO  

pyramid(4, '*', upside_down=True, offset=3)
#    * * * * 
#     * * * 
#      * * 
#       * 


scleronomic
  • 4,392
  • 1
  • 13
  • 43
-3

x=input('enter some numerical value')

s=''

for i in range(0,x):

     for j in range(0,i+1):

              s=s+str(j+1)
     print s
     s=''
Raj Damani
  • 782
  • 1
  • 6
  • 19