Pankaj
Pankaj

Reputation: 65

to print a triangle of hash '#' and stars '*' in python

I m trying to print the following triangle in python, but not able to

*#####
**####
***###
****##
*****#

I am only able to print '*', but not hashes.

here's my code to print '*'

max = 6
for x in range(1,max+1):
    for y in range(1,x+1):
        print '*',

    print

Upvotes: 0

Views: 4538

Answers (2)

Sekhar C
Sekhar C

Reputation: 188

How to Print * in a triangle shape in python?

      *
     * *
    * * *
   * * * *
  * * * * *

This is one of the coding interview questions.

def print_triangle(n):
    for i, j in zip(range(1,n+1), range(n,0,-1)):
       print(" "*j, '* '*i, " "*j)
print_triangle(5)

Upvotes: 0

Manuel
Manuel

Reputation: 706

length = 6
for x in range(1, length+1):
    print(x * '*' + (length - x) * '#')  # integer * (string), concatenate the string integer times

Upvotes: 7

Related Questions