溫馨提示×

python怎么打印在同一行

小億
84
2024-09-25 23:21:07
欄目: 編程語言

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

0