如何在多行中打印元组?

发布于 2025-01-20 07:07:51 字数 299 浏览 0 评论 0原文

字典元素

“”

如何在多行中正确打印它?

当我做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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酸甜透明夹心 2025-01-27 07:07:51

简单的解决方案是将字符串与换行符连接起来,或者只是迭代并打印每个字符串

DICE_ART = {
    1: ('┌─────────┐',
        '│         │',
        '│    ●    │',
        '│         │',
        '└─────────┘')
}

print("\n".join(DICE_ART[1]))

for row in DICE_ART[1]:
    print(row)

Simple solution would be to join the strings with a newline char, or just iterate and print each

DICE_ART = {
    1: ('┌─────────┐',
        '│         │',
        '│    ●    │',
        '│         │',
        '└─────────┘')
}

print("\n".join(DICE_ART[1]))

for row in DICE_ART[1]:
    print(row)
甜心小果奶 2025-01-27 07:07:51

您希望元组中的每个字符串之间有一个换行符。因此,您应该使用:

"\n".join(DICE_ART[<dice_value>])

其中 是您要打印的骰子的面值。

You want a newline between each string in the tuple. So, you should use:

"\n".join(DICE_ART[<dice_value>])

where <dice_value> is the face value of the die you want to print.

别低头,皇冠会掉 2025-01-27 07:07:51

作为使用joinsep的替代方向包含您要打印的实际字符串 - 如果您永远不会使用这些涉及单独迭代的字符串(除了与Newlines一起加入),那么它们没有理由使它们在该字符中成为多个不同的字符串首位。

DICE_ART = {
    1: (
        '┌─────────┐\n'
        '│         │\n'
        '│    ●    │\n'
        '│         │\n'
        '└─────────┘'
    ),
    # ...
}

print(DICE_ART[1])

请注意,上面示例中的帕伦斯仅出于格式目的,并且由于没有逗号而不会使价值成为元组;将字符串串联成一个包含新线的单字符串。

(还有许多其他方法可以安排此字符串,包括带有“”“”的多行字母文字,我敢肯定,人们会有很多评论,证明他们知道所有这些方式。在我知道如何做的所有方式中,我认为这是使代码易于视觉扫描的最佳选择,同时以所需的格式产生数据。

As an alternate direction from using join or sep 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.

DICE_ART = {
    1: (
        '┌─────────┐\n'
        '│         │\n'
        '│    ●    │\n'
        '│         │\n'
        '└─────────┘'
    ),
    # ...
}

print(DICE_ART[1])

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.)

神魇的王 2025-01-27 07:07:51

如果不是必须使用元组,请尝试文本块多行字符串:

DICE_ART = {
    1: """
    ┌─────────┐
    │         │
    │    ●    │
    │         │
    └─────────┘
    """,
    2: """
    ┌─────────┐
    │ ●       │
    │         │
    │       ● │
    └─────────┘
    """,
    ...
}

If use of tuples is not a must, try text blocks for multi-line strings:

DICE_ART = {
    1: """
    ┌─────────┐
    │         │
    │    ●    │
    │         │
    └─────────┘
    """,
    2: """
    ┌─────────┐
    │ ●       │
    │         │
    │       ● │
    └─────────┘
    """,
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文