如何在三个单独的行中打印格式化语句?
How to print formatted statement in three separate lines?
我有一个格式化的打印语句,我想打印成三行。代码如下:
sender = 'tim@abc.com'
recipient = 'tom@xyz.org'
subject = 'Hello!'
print('From: {} To: {} subject: {}'.format(sender, recipient, subject))
目前打印如下:
From: tim@abc.com To: tom@xyz.org subject: Hello!
但我希望它打印成这样:
From: tim@abc.com
To: tom@xyz.org
subject: Hello!
我想我可以让它打印出来,就像我在打印语句中放置一个 "sep" 函数一样:
sender = 'tim@abc.com'
recipient = 'tom@xyz.org'
subject = 'Hello!'
print('From: {} To: {} subject: {}'.format(sender, recipient, subject), sep = '\n')
虽然没有用。有任何想法吗?感谢所有帮助!
sep
关键字仅适用于单独的字符串,如下所示:
>>> print('From: {}', 'To: {}', 'subject: {}', sep = '\n')
From: {}
To: {}
subject: {}
对于集成字符串,只需在字符串中添加新行 \n
字符:
>>> print('From: {} \nTo: {} \nsubject: {}'.format(sender, recipient, subject))
From: tim@abc.com
To: tom@xyz.org
subject: Hello!
直接在打印字符串中使用'\n'
a ="aaa"
b="bbb"
c="ccc"
print('From: {}\nTo: {}\nSubject:{}\n'.format(a,b,c))
我知道了:
From: aaa
To: bbb
Subject:ccc
在您的原始设置中,您只有一个字符串,因此没有任何分隔符(即 sep)可以分隔。
我有一个格式化的打印语句,我想打印成三行。代码如下:
sender = 'tim@abc.com'
recipient = 'tom@xyz.org'
subject = 'Hello!'
print('From: {} To: {} subject: {}'.format(sender, recipient, subject))
目前打印如下:
From: tim@abc.com To: tom@xyz.org subject: Hello!
但我希望它打印成这样:
From: tim@abc.com
To: tom@xyz.org
subject: Hello!
我想我可以让它打印出来,就像我在打印语句中放置一个 "sep" 函数一样:
sender = 'tim@abc.com'
recipient = 'tom@xyz.org'
subject = 'Hello!'
print('From: {} To: {} subject: {}'.format(sender, recipient, subject), sep = '\n')
虽然没有用。有任何想法吗?感谢所有帮助!
sep
关键字仅适用于单独的字符串,如下所示:
>>> print('From: {}', 'To: {}', 'subject: {}', sep = '\n')
From: {}
To: {}
subject: {}
对于集成字符串,只需在字符串中添加新行 \n
字符:
>>> print('From: {} \nTo: {} \nsubject: {}'.format(sender, recipient, subject))
From: tim@abc.com
To: tom@xyz.org
subject: Hello!
直接在打印字符串中使用'\n'
a ="aaa"
b="bbb"
c="ccc"
print('From: {}\nTo: {}\nSubject:{}\n'.format(a,b,c))
我知道了:
From: aaa
To: bbb
Subject:ccc
在您的原始设置中,您只有一个字符串,因此没有任何分隔符(即 sep)可以分隔。