【python小知识】关于Python中(\t、\n、end=‘ ‘)的意思及99乘法表

\t表示空4个字符,就是缩进,就是按一下tab键
\n表示换行
end=''表示末尾不换行

【python小知识】关于Python中(\t、\n、end=‘ ‘)的意思及99乘法表_第1张图片

 

打印99乘法表

代码

#coding:utf-8
#方法1:format 函数可以接受不限个参数,位置可以不按顺序。
#https://www.runoob.com/python/att-string-format.html
#https://www.runoob.com/python3/python3-99-table.html参考
#有个参数为end='',默认是 \n,end='\n'因此默认会进行换行操作,像下面的例子,为了不换行,只能指定end='',我们这里指定end='   '

for a in range(1,10):
    for b in range(1,a+1):
        print('{}*{}={}\t'.format(b,a,a*b),end='')
        #print('{}*{}={}'.format(b, a, a * b),end='  ')  #用这个也可以
        #print(f'{b}*{a}={a*b}', end='  ')  #用这个也可以
    print()


#方法2:https://cloud.tencent.com/developer/article/1559802   老方法
#Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。
#基本语法是通过 {} 和 : 来代替以前的 % 。

for a in range(1,10):
    for b in range(1,a+1):
        print('%d*%d=%d' % (b,a,a*b),end='   ')
    print()

 

执行结果

【python小知识】关于Python中(\t、\n、end=‘ ‘)的意思及99乘法表_第2张图片

你可能感兴趣的:(Python,python,九九乘法表,python,99乘法表)