rose
rose

Reputation: 31

How do I print stars and a sequence of numbers?

I am trying to print:

* * * *
* * * 1
* * 1 2
* 1 2 3 

I have tried these codes:

n = 3
for i in range(1, n+1)
   print(str("* "*(n-i)) + str(x for x in range(1, n+1)))

but the output says generator.

Thanks!

Upvotes: 0

Views: 126

Answers (1)

Rupesh
Rupesh

Reputation: 538

n = 3
for i in range(0, n+1):
    print(str("* "*(n-i+1)) + ' '.join([str(x) for x in range(1, i+1)]))

Output:

* * * * 
* * * 1
* * 1 2
* 1 2 3

The way to generate such a concatenation of string is to make a list of strings using list comprehension and then use join function to join all the element of the list

Upvotes: 3

Related Questions