在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.