Asterisk art in python

I would like to create this photo in python!

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

I introduced this:

x=1
while x<10:
 print '%10s'    %'*'*x
 x=x+1

Which, unfortunately, creates something consisting of the correct number of points, like the image above, but each of these stars is divided at a distance from each other, and is not justified as a whole as a whole.

Someone has a smart mind, how can I achieve what I want?

+5
source share
5 answers
 '%10s'    %'*'*x

analyzed as

('%10s' % '*') * x

since the operators %and *have the same priority and the group from left to right [ docs ] . You need to add parentheses, for example:

x = 1
while x < 10:
    print '%10s' % ('*' * x)
    x = x + 1

, for, while. :

for x in range(1, 10):
    print '%10s' % ('*' * x)

for x in range(0, 10) for(int x = 0; x < 10; x++) Java C.

+13

rjust ljust .

>>> n = 10
>>> for i in xrange(1,n+1):
...   print (i*'*').rjust(n)
... 
         *
        **
       ***
      ****
     *****
    ******
   *******
  ********
 *********
**********

, :

>>> for i in reversed(xrange(n)):
...   print (i*' ').ljust(n, '*')
... 
         *
        **
       ***
      ****
     *****
    ******
   *******
  ********
 *********
**********

* .

ljust rjust - . , , . print '--Spam!'.ljust(80, '-').

+8

- , :

x=1
while x<10:
 print '%10s' % ('*'*x)
 x=x+1
+2
print '\n'.join(' ' * (10 - i) + '*' * i for i in range(10))
+1

, 10 , .

for i in range(1, 11):
    print "%10s"%('*' *i)
+1

All Articles