我想打印其他文本旁邊一些文本已經在Python 之前打印例如之間的換行符打印照片,無需兩串
print("Hello")
a="This is a test"
print(a)
我的意思是打印這樣的「HelloThis是一個測試」不在下一行我知道我應該使用打印(「你好」,一),但我想使用分離的打印命令!
我想打印其他文本旁邊一些文本已經在Python 之前打印例如之間的換行符打印照片,無需兩串
print("Hello")
a="This is a test"
print(a)
我的意思是打印這樣的「HelloThis是一個測試」不在下一行我知道我應該使用打印(「你好」,一),但我想使用分離的打印命令!
在第一print
呼叫使用end=''
:
print("Hello", end='')
a = "This is a test"
print(a)
#HelloThis is a test
幫助上print
:
print(value, ..., sep=' ', end='\n', file=sys.stdout)
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.
如果您正在使用Python 2.7(對問題蟒蛇標籤),你可以在打印後放置一個逗號以不返回新行。
print("hello"),
print("world")
將打印「helloworld」全部一行。 所以你的情況將是:
print("Hello"),
print(a)
或者,如果你使用Python 3(對問題python3.x標籤)使用方法:
print("hello", end='')
print('world')
所以你的情況將是:
print("Hello", end='')
print(a)
不在Python 3.x中,這是他正在使用的。 – iCodez
'print(「Hello%s」%a)'也可以使用 – 2013-10-20 16:51:52
或使用新的(首選的)'.format'語法的print(「Hello {} .format(a))。 – SethMMorton