本文 示范了 python编写99乘法表(主要是对代码的理解和实现的算法上,适合 已经入门的算法设计人员参考)
for m in range(1,10):
print('',end="|")
for n in range(1, m + 1):
print('{:-<11}'.format('-'), end='|')
print()
for n in range(1,m+1):
print("| {0:>1} * {1:>1} = {2:>2}".format(n, m, m*n), end='')
print("|")
for n in range(1, m + 1):
print('|{:-<11}'.format('-'), end='')
print('',end="|")
效果如下:
主要是到了python中 print的用法:
#通过关键字
print('{str1}今天{str2}'.format(str1='111',str2='222'))
#通过关键字,可用字典当关键字传入值时,在字典前加**即可
example = {'str1': '111', 'str2': '222'}
print('{str1}AAAAAA{str2}'.format(**example))
#填充和对齐^<>分别表示居中、左对齐、右对齐,后面带宽度,冒号后面是填充的字段
print('{:^14}'.format('AAA'))
print('{:>14}'.format('AAA'))
print('{:<14}'.format('AAA'))
print('{:*<14}'.format('AAA'))
print('{:*>14}'.format('AAA'))