Rohit  Singhal
Rohit Singhal

Reputation: 41

Program in Python for alphabets in pattern

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

Upvotes: 3

Views: 47283

Answers (8)

Krishnendu Biswas
Krishnendu Biswas

Reputation: 1

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.

Upvotes: 0

MANISH RAWAT
MANISH RAWAT

Reputation: 81

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("")

Upvotes: 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

Upvotes: 0

Amit Singh
Amit Singh

Reputation: 1

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  

Upvotes: -2

VIJAYAKUMAR R
VIJAYAKUMAR R

Reputation: 1

  • 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
    

Upvotes: 0

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

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)))

Upvotes: 3

jgritty
jgritty

Reputation: 11915

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

Upvotes: 5

Jakube
Jakube

Reputation: 3565

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?

Upvotes: 1

Related Questions