liuyisi 发表于 2020-4-7 17:07:35

python课后疑问

>>> for i in range(1, 10):
...   for j in range(1, i+1):
...         print(i, "x", j, "=", i * j, end=' ')
...   print("\n")
#end='',这是什么意思

zltzlt 发表于 2020-4-7 17:08:24

就是打印完内容后以一个空格结尾,不以换行结尾。

具体参考:Python FAQ 013 print 函数的 end 参数

老八秘制 发表于 2020-4-7 17:13:22

默认多个print语句的输出每条语句都会换行,end=' '的意思是让它在打印完一句之后把这一句和下一句用空格隔开

raimond 发表于 2020-4-7 17:26:43

按住Ctrl 然后点击 print

可以看到
def print(self, *args, sep=' ', end='\n', file=None): # known special case of 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.
    """
    pass

python 内置默认 end='\n"
\n 是换行符号, 你可以替换其他的

print('abc', end='python')#你运行这个看下

杜若左 发表于 2020-4-7 17:48:06

如果在print语句中使用这个东西,相当于就是不换行的意思。
希望楼主可以赏赐我一个最佳答案。{:10_254:}
页: [1]
查看完整版本: python课后疑问