在Python中,您可以使用end参数在print函数中指定行尾字符。默认情况下,end参数的值是换行符\n,这意味着每次调用print函数时,都会在输出中添加一个新行。
如果您想在同一行上打印多个项目,可以将end参数设置为空字符串''或空格' '。以下是一些示例:
使用空字符串'':
print('Hello', 'world!', end='')
print('We are on the same line.')
输出:
Hello world! We are on the same line.
使用空格' ':
print('Hello', 'world!', end=' ')
print('We are on the same line.')
输出:
Hello world! We are on the same line.