2013-10-20 62 views
1

我想打印其他文本旁邊一些文本已經在Python 之前打印例如之間的換行符打印照片,無需兩串

print("Hello") 
a="This is a test" 
print(a) 

我的意思是打印這樣的「HelloThis是一個測試」不在下一行我知道我應該使用打印(「你好」,一),但我想使用分離的打印命令!

+0

'print(「Hello%s」%a)'也可以使用 – 2013-10-20 16:51:52

+0

或使用新的(首選的)'.format'語法的print(「Hello {} .format(a))。 – SethMMorton

回答

5

在第一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. 
+0

in'Python-3.x' – dansalmo

+0

@dansal mo問題中有一個python-3.x標籤。 –

+0

問題上還有一個Python標籤。 – dansalmo

-1

如果您正在使用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) 
+1

不在Python 3.x中,這是他正在使用的。 – iCodez