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
  • 13,587
  • 3
  • 35
  • 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,536
  • 3
  • 21
  • 38
jgritty
  • 11,242
  • 3
  • 35
  • 58
  • 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
  • 168,988
  • 22
  • 228
  • 312
  • 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
  • 2,858
  • 2
  • 20
  • 39
  • 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,000
  • 34
  • 174
  • 243
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,323
  • 15
  • 26
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("")
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
  • 5,918
  • 3
  • 41
  • 52