首页 > python中print语句问题

python中print语句问题

for row in range(1,10):

for col in range(1,10):
    prod=row*col
    if prod<10:
        print(' ',end='')
    print(row*col,' ',end='')
print()

如果不加if prod<10:
        print(' ',end='')
显示效果不好,但是加的这两行代码什么意思呢? end作用时啥?

Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

end参数默认是换行符,上面两行代码的作用是打印小于10的数字多打印一个空格,保证格式整齐。


防止换行,因为默认print会结束一行

【热门文章】
【热门文章】