【问题标题】:Can I use line breaks in a python format funtion print line?我可以在 python 格式函数打印行中使用换行符吗?
【发布时间】:2020-01-20 15:34:35
【问题描述】:

这是我要拆分的代码行,它工作正常,但它是一个很长的行,如何在这样的格式函数打印行中使用换行符?

return '{0.name} is a {0.sex} rabbit and has {0.fur_colour} fur and {0.eye_colour} eyes, {0.name} is of the {0.breed} breed of rabbits.\n{0.name} is '.format(paul)+ disp_age

【问题讨论】:

  • 那只是一个字符串,不是打印行函数。你可以在总和分手,在行尾做\
  • 感谢您的帮助。

标签: python printing formatting line-breaks


【解决方案1】:

您可以将整个拆分后的字符串存储到()

return ("{0.name} is a {0.sex} rabbit"
    " and has {0.fur_colour} fur and "
    "{0.eye_colour} eyes, {0.name} is "
    "of the {0.breed} breed of rabbits.\n"
    "{0.name} is ").format(paul)+ disp_age

这个关于多行字符串的答案很有用: https://stackoverflow.com/a/10660443/7042357

【讨论】:

  • 是的,这是我正在寻找的解决方案,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多