如何在多行中打印元组?
字典元素
如何在多行中正确打印它?
当我做print(dice_art [1])
时,我会得到这样的字符串:
('┌─────────┐', '│ │', '│ ● │', '│ │', '└─────────┘')
Dictionary element
How do I properly print this in multiple lines?
When I do print(DICE_ART[1])
I get such string:
('┌─────────┐', '│ │', '│ ● │', '│ │', '└─────────┘')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简单的解决方案是将字符串与换行符连接起来,或者只是迭代并打印每个字符串
Simple solution would be to join the strings with a newline char, or just iterate and print each
您希望元组中的每个字符串之间有一个换行符。因此,您应该使用:
其中
是您要打印的骰子的面值。You want a newline between each string in the tuple. So, you should use:
where
<dice_value>
is the face value of the die you want to print.作为使用
join
或sep
的替代方向包含您要打印的实际字符串 - 如果您永远不会使用这些涉及单独迭代的字符串(除了与Newlines一起加入),那么它们没有理由使它们在该字符中成为多个不同的字符串首位。请注意,上面示例中的帕伦斯仅出于格式目的,并且由于没有逗号而不会使价值成为元组;将字符串串联成一个包含新线的单字符串。
(还有许多其他方法可以安排此字符串,包括带有
“”“”
的多行字母文字,我敢肯定,人们会有很多评论,证明他们知道所有这些方式。在我知道如何做的所有方式中,我认为这是使代码易于视觉扫描的最佳选择,同时以所需的格式产生数据。As an alternate direction from using
join
orsep
to join the strings into the desired format at the time you print them, I'll suggest that you arrange your dict so that it contains the actual strings you want to print -- if you're never going to do anything with these strings that involves iterating over them individually (other than to join them with newlines), there's no reason for them to be multiple distinct strings in the first place.Note that the parens in the above example are solely for formatting purposes and do not make the value a tuple because there are no commas; the strings are concatenated together into a single string that contains newlines.
(There are lots of other ways to arrange this string, including multiline string literals with
"""
, and I'm sure there'll be lots of comments from people demonstrating that they know all those ways. Of all the ways I know how to do it, I think this one is the best-looking option in terms of making the code easy to scan visually while producing data in the desired format.)如果不是必须使用元组,请尝试文本块多行字符串:
If use of tuples is not a must, try text blocks for multi-line strings: