3

I have written this program:

for i  in range(1,6):
    for j in range(65,65+i):
        a =  chr(j)
        print (a)
    print

I want to print a pattern as follows:

A
A B
A B C
A B C D
A B C D E

but i am not getting the desired output

i am getting

A

A
B

A
B
C

A
B
C
D

A
B
C
D
E
elo80ka
  • 14,837
  • 3
  • 36
  • 43
Rohit Singhal
  • 41
  • 1
  • 1
  • 4

8 Answers8

5

In python 2, simply put a comma after the print statement:

for i in range(1, 6):
    for j in range(65, 65+i):
        a = chr(j)
        print a,
    print

For python 3, or in python 2 using from __future__ import print_function you would do something like this:

for i in range(1, 6):
    for j in range(65, 65+i):
        a = chr(j)
        print(a, end=" ")
    print()

Note that I put a space (" ") as the end character. You could set it to "" and the output will be without spaces, like so:

A
AB
ABC
ABCD
ABCDE
Sebastian Kreft
  • 7,819
  • 3
  • 24
  • 41
jgritty
  • 11,660
  • 3
  • 38
  • 60
  • with `from __future__ import print_function` the Python 3 version, will also work in Python 2.6 and newer. – AlexV Dec 21 '15 at 19:50
3

You can also use str.join using string.ascii_uppercase:

from string import ascii_uppercase
for i in range(1, 6):
    print(" ".join(ascii_uppercase[:i]))

Or using your range logic:

for i in range(1, 6):
    print(" ".join(chr(j) for j in range(65, 65 + i)))
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • 1
    I was just typing this answer, mostly because constructing the string A-E and printing it in one go is much faster than calling print 6 times to print each letter individually and a newline. – Reti43 Dec 21 '15 at 20:23
1

print a (or print (a)) will print a newline. If you want to suppress the newline you can write

print a,

For more infos see the question: Printing without newline (print 'a',) prints a space, how to remove?

Community
  • 1
  • 1
Jakube
  • 3,353
  • 3
  • 23
  • 40
  • There is also a nice SO answer which differentiates by the different Python versions [here](http://stackoverflow.com/a/493399/5682996) – AlexV Dec 21 '15 at 19:46
0
  • Just put a comma after print(a) in python 3.
  • In python 2 you just put end=" " inside of print(a,end=" ")

    #!usr/bin/env python
    for i  in range(1,6):
        for j in range(65,65+i):
            a =  chr(j)
            print (a),
            print
    
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
0
n=int(input())
for i in range(1,n+1):
    print(" ")
    for j in range(65,65+i):
        a=chr(j)
        print(a,end=" ")
    print

Aymen
  • 1,476
  • 18
  • 28
0

The problem with your code is that you are not using end=" " in print statement, it is required to use end=" " because python print() function by default print in new line, thats why at every iteration it is jumping into new line. Correction to your code is:

for i in range(1,6):
    print(" ")
    for j in range(65,65+i):
        print(chr(j),end=" ")
    print("")
MANISH RAWAT
  • 81
  • 1
  • 2
0
for i  in range(1,6):
for j in range(65,65+i):
    a =  chr(j)
    print (a, end = " ")
print()

Here, the modification is in the first print statement where I just have added the end parameter.

-2
for i  in range(1,6):
    for j in range(65,65+i):
        a =  chr(j)
        print (a)
        print("")

output:

C

A                                                                                                                               

B                                                                                                                               

C                                                                                                                               

D                                                                                                                               

A                                                                                                                               

B                                                                                                                               

C                                                                                                                               

D                                                                                                                               

E  
George
  • 6,886
  • 3
  • 44
  • 56