Asterisk art in python

前端 未结 5 1094
遇见更好的自我
遇见更好的自我 2020-12-20 21:10

I would like to produce this picture in python!

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


        
5条回答
  •  一向
    一向 (楼主)
    2020-12-20 22:11

    string object has rjust and ljust methods for precisely this thing.

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

    or, alternatively:

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

    My second example uses a space character as the printable character, and * as the fill character.

    The argument to ljust or rjust is the terminal width. I often use these for separating sections with headings when you have chatty debug printout, e.g. print '--Spam!'.ljust(80, '-').

提交回复
热议问题