spacing for triangle pattern in python
<p>The following code prints a pattern but I have problem with spacing. I want to print it in a triangle fashion    </p>

<pre><code>def triangle(n): 
    s = ""
    for i in range(0,n):
        s +=  "{}".format((i+1)%10)
        j=s*1
        print( s,'*1=',j)


triangle(9)
</code></pre>

<p>This is the output I get</p>

<pre class="lang-none prettyprint-override"><code>1 *1= 1
12 *1= 12
123 *1= 123
1234 *1= 1234
12345 *1= 12345
123456 *1= 123456
1234567 *1= 1234567
12345678 *1= 12345678
123456789 *1= 123456789
</code></pre>

<p>If you meen this</p>

<pre><code>         1 * 1 = 1
        12 * 1 = 12
       123 * 1 = 123
      1234 * 1 = 1234
     12345 * 1 = 12345
    123456 * 1 = 123456
   1234567 * 1 = 1234567
  12345678 * 1 = 12345678
 123456789 * 1 = 123456789
</code></pre>

<p>then you need ie. <code>{:9s}</code> (for <code>string</code>) to add extra spaces (or <code>{:9d}</code> for <code>int</code>)</p>

<pre><code>def triangle(n): 
    a = 0
    b = 1
    for i in range(1, n+1):
        a = 10*a + i
        print('{:9d} * {} = {}'.format(a, b, a*b))

triangle(9)
</code></pre>

<p>See: <a href="https://pyformat.info/" rel="nofollow">pyformat.info</a></p>

<hr>

<p><strong>EDIT:</strong> to use <code>n</code> instead of <code>9</code> you need <code>{:{}d}</code> and <code>.format(a, n, ...)</code></p>

<pre><code>    print('{:{}d} * {} = {}'.format(a, n, b, a*b))
</code></pre>

<hr>

<p><strong>EDIT:</strong> version with two arguments</p>

<pre><code>def triangle(n, b): 
    a = 0
    for i in range(1, n+1):
        a = 10*a + i
        print('{:{}d} * {} = {}'.format(a, n, b, a*b))

triangle(5, 3)

    1 * 3 = 3
   12 * 3 = 36
  123 * 3 = 369
 1234 * 3 = 3702
12345 * 3 = 37035
</code></pre>

<hr>

<p><strong>EDIT:</strong> some explanations:</p>

<p><strong>Line</strong> <code>a = 10*a + i</code>:</p>

<p>You used string and concatenation to create string <code>"1234"</code> from string <code>"123"</code> - <code>"1234" = "123" + "4"</code>. I use integer and <code>*10</code> to create integer <code>1234</code> from integer <code>123</code> - <code>1234 = 123*10 + 4</code>. (and now I can use integer <code>1234</code> to calcutate <code>1234*b</code> in next line)</p>

<p><strong>Line</strong> <code>print('{:{}d} * {} = {}'.format(a, n, b, a*b))</code></p>

<p>To make more readable you can use </p>

<ul>
<li><p>numbers (because arguments in <code>format()</code> are numbered - 0,1,2,...)</p>

<pre><code>print('{0:{1}d} * {2} = {3}'.format(a, n, b, a*b))
</code></pre>

<p>or even (<code>d</code> means <code>decimal</code> or <code>int</code>)</p>

<pre><code>print('{0:{1:d}d} * {2:d} = {3:d}'.format(a, n, b, a*b))
</code></pre></li>
<li><p>names</p>

<pre><code>print('{x:{y}d} * {z} = {v}'.format(x=a, y=n, z=b, v=a*b))
</code></pre>

<p>or even</p>

<pre><code>print('{x:{y:d}d} * {z:d} = {v:d}'.format(x=a, y=n, z=b, v=a*b))
</code></pre></li>
</ul>

<p><code>{:9}</code> (or <code>{x:9d}</code>) will get integer value and create 9 char length text aligned to right so you get extra spaces on left side. </p>

<p>Try <code>{:&lt;9}</code> to get 9 char length text aligned to left </p>

<pre><code>1         * 3 = 3
12        * 3 = 36
123       * 3 = 369
1234      * 3 = 3702
12345     * 3 = 37035
123456    * 3 = 370368
1234567   * 3 = 3703701
12345678  * 3 = 37037034
123456789 * 3 = 370370367
</code></pre>

<p>or <code>{:^9}</code> to get centered text</p>

<pre><code>    1     * 3 = 3
   12     * 3 = 36
   123    * 3 = 369
  1234    * 3 = 3702
  12345   * 3 = 37035
 123456   * 3 = 370368
 1234567  * 3 = 3703701
12345678  * 3 = 37037034
123456789 * 3 = 370370367
</code></pre>

<p>See more: <a href="https://pyformat.info/" rel="nofollow">pyformat.info</a></p>

python string-formatting