在两个字符串之间没有换行符打印

时间:2013-10-20 16:47:51

标签: python text python-3.x printing

我想在python之前打印的另一个文本旁边打印一些文本 例如

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

我的意思是打印像这样的“HelloThis是一个测试”而不是下一行我知道我应该使用print(“Hello”,a)但是我想使用分离的打印命令!!!!

2 个答案:

答案 0 :(得分:5)

在第一次end=''来电中使用print

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.

答案 1 :(得分:-1)

如果您使用的是python 2.7(有问题的python标签),您可以在打印后放置逗号以不返回新行。

print("hello"),
print("world")

将打印“helloworld”全部一行。 所以在你的情况下它将是:

print("Hello"),
print(a)

或者如果你使用python 3(问题上的python3.x标签)使用:

print("hello", end='')
print('world')

所以在你的情况下它将是:

print("Hello", end='')
print(a)
相关问题