Reputation: 31
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
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