簡體   English   中英

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

[英]How to print two strings on different lines with print()

我想在兩行第一行“你好”第二行“ 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中運行時得到了這個

你好,塞西爾

我希望它像這樣打印:

哈羅,歡迎來到書店
我是塞西爾,讓我知道您是否需要幫助

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
print(f"{greeting}\n{Cecil}") # f-string
# or
print(greeting + "\n" + Cecil) # concatenation

這兩個選項都會輸出您想要的。 f"{variable}""{}".format(variable)相同

如下代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM