python追加写换行_是否可以向.format()方法添加换行符?

我读了一本教科书,遇到了一个有趣的问题,让我用print语句打印一个地址:John Doe

123 Main Street

AnyCity, AS 09876

我正在尝试使用oneprint语句来确定是否可以这样做,但是我不知道如何在Python3中使用.format()方法添加换行符。我试过的是:>>> first = 'John'

>>> last = 'Doe'

>>> street = 'Main Street'

>>> number = 123

>>> city = 'AnyCity'

>>> state = 'AS'

>>> zipcode = '09876'

>>>

>>> ("{0} {1}\n{2} {3}\n{4}, {5} {6}").format(first, last, number, street, city, state, zipcode)

'John Doe\n123 Main Street\nAnyCity, AS 09876'

>>>

>>> ("{0} {1}'\n'{2} {3}'\n'{4}, {5} {6}").format(first, last, number, street, city, state, zipcode)

"John Doe'\n'123 Main Street'\n'AnyCity, AS 09876"

>>>

>>> ("{0} {1}{7}{2} {3}{8}{4}, {5} {6}").format(first, last, number, street, city, state, zipcode, '\n', '\n')

'John Doe\n123 Main Street\nAnyCity, AS 09876'

>>>

>>> ("{0} {1} \n {2} {3} \n {4}, {5} {6}").format(first, last, number, street, city, state, zipcode)

'John Doe \n 123 Main Street \n AnyCity, AS 09876'

这可能是一个非常简单的问题,我只是遗漏了一些非常基本的问题。谢谢你的帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值