首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用print()在不同的行上打印两个字符串

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

Stack Overflow用户
提问于 2019-07-24 00:05:05
回答 3查看 55关注 0票数 0

我想把这个书店的问候语打印在两行第一行“hallo”第二行“Cecil”上

代码语言:javascript
复制
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中运行它的时候,我得到了这个

hallo n\ Cecil

我希望它像这样打印:

你好,欢迎光临书店

我是塞西尔,如果你需要帮助找什么请告诉我

EN

回答 3

Stack Overflow用户

发布于 2019-07-24 00:08:14

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2019-07-24 00:39:34

代码语言:javascript
复制
print(f"{greeting}\n{Cecil}") # f-string
# or
print(greeting + "\n" + Cecil) # concatenation

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

票数 0
EN

Stack Overflow用户

发布于 2019-07-24 04:04:07

以下代码:

代码语言:javascript
复制
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)

生成以下输出:

代码语言:javascript
复制
3
5
7
Hallo, welcome to bookstore
I'm Cecil let me know if you need help finding anything
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57168185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档