python implements printing multiplication formula

 1 minute to read

python implements printing multiplication formula

Involved syntax range and str.format()

Function syntax: range(start, stop[, scan])

Parameter Description:

  • start: Counting starts from start. The default is to start from 0. For example range(5) is equivalent to range(0, 5);
  • stop: Count up to the end of stop, but not including stop. For example: range(0,5) range is [0, 1, 2, 3, 4] not 5
  • scan: The spacing between each jump, the default is 1. For example: range(0,5) is equivalent to range(0, 5, 1)

{2:<2} means the third element has a left-aligned width of 2

def print_cheng_fa():
     for i in range(1, 10):
         s = ""
         for j in range(1, i + 1):
             s += str.format("{0:1}x{1:1}={2:<2} ", j, i, i * j)
         print(s)


if __name__ == '__main__':
     print_cheng_fa()