如何使用print()在不同的行上打印两个字符串

时间:2019-07-23 16:05:05

标签: python pycharm

我想在两行第一行“你好”第二行“ Cecil”上打印此书店的问候

for x in range(3,8,2):
	print(x)
	
system = 'bookstore'
greeting = 'Hallo, welcome to ' + str(system)
Cecil = " I'm Cecil let me know if you need help finding anything"

hallo = greeting

print('hallo' \n + 'Cecil')

我在pycharm中运行时得到了它

你好n \塞西尔

我希望它像这样打印:

你好,欢迎来到书店
我叫塞西尔(Cecil),让我知道您是否需要任何帮助

3 个答案:

答案 0 :(得分:0)

system = 'bookstore'
greeting = 'Hallo, welcome to {}'.format(system) 
Cecil = " I'm Cecil let me know if you need help finding anything"
hallo = greeting

print('{}\n{}'.format(hallo, Cecil))


Hallo, welcome to bookstore
I'm Cecil let me know if you need help finding anything

答案 1 :(得分:0)

print(f"{greeting}\n{Cecil}") # f-string
# or
print(greeting + "\n" + Cecil) # concatenation

两个选项都将输出您想要的内容。 f"{variable}""{}".format(variable)

答案 2 :(得分:0)

以下代码:

for x in range(3,8,2):
    print(x)

system = 'bookstore'
greeting = 'Hallo, welcome to ' + str(system)
Cecil = "I'm Cecil let me know if you need help finding anything"

hallo = greeting

print(hallo + '\n' + Cecil)

产生以下输出:

3
5
7
Hallo, welcome to bookstore
I'm Cecil let me know if you need help finding anything