在Python中,您可以使用end
參數(shù)在print
函數(shù)中指定行尾字符。默認(rèn)情況下,end
參數(shù)的值是換行符\n
,這意味著每次調(diào)用print
函數(shù)時(shí),都會(huì)在輸出中添加一個(gè)新行。
如果您想在同一行上打印多個(gè)項(xiàng)目,可以將end
參數(shù)設(shè)置為空字符串''
或空格' '
。以下是一些示例:
使用空字符串''
:
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.