Python f-string:长字符串的缩进?

发布于 2025-01-13 07:13:01 字数 914 浏览 2 评论 0原文

我在 Python 中面临长 f 字符串表达式的(审美)问题。例如,此代码片段

if __name__ == '__main__': 
    x_true = 3
    x_prov = 4

    assert x_prov == x_true, f'The provided value for x, namely {x_prov}, is invalid, \
    the true value should be {x_true}!'

给出以下错误,当然,这完全是预料之中的:

AssertionError: The provided value for x, namely 4, is invalid,     the true value should be 3!

错误消息中有一个不需要的缩进,但在我看来,它看起来非常难看。避免这种情况的一种方法是编写

if __name__ == '__main__': 
    x_true = 3
    x_prov = 4

    assert x_prov == x_true, f'The provided value for x, namely {x_prov}, is invalid, \
the true value should be {x_true}!'
    
    # Do sth else. 

然而,这对我来说也很难看,因为现在字符串的第二行与外部 '__main__' 函数具有相同的缩进。有没有办法让 AssertionError 正确显示,而无需将代码缩进为外部函数?

I'm facing (aesthetic) problems with long f-string expressions in Python. For example, this code snippet

if __name__ == '__main__': 
    x_true = 3
    x_prov = 4

    assert x_prov == x_true, f'The provided value for x, namely {x_prov}, is invalid, \
    the true value should be {x_true}!'

gives the following error, which is totally expected, of course:

AssertionError: The provided value for x, namely 4, is invalid,     the true value should be 3!

There is an unwanted indentation in the error message that in my opinion looks very ugly, though. One way to circumvent this is to write

if __name__ == '__main__': 
    x_true = 3
    x_prov = 4

    assert x_prov == x_true, f'The provided value for x, namely {x_prov}, is invalid, \
the true value should be {x_true}!'
    
    # Do sth else. 

However, this also looks ugly to me, as now the second line of the string has the same indentation as the outer '__main__' function. Is there a way to have the AssertionError displayed properly without having to indent the code as the outer function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

不及他 2025-01-20 07:13:01

有2种情况可以解决你的问题。

  • 创建带括号的表达式。
    示例:
(f"First part of f-string"
 f"Second part of f-string")
  • 在 f 字符串后创建一个带有 \ 分隔符的多个字符串。
    例子:
f"Some_part" \
f"Another part"

There are 2 cases to solve your problem.

  • Create an expression with brackets.
    Example:
(f"First part of f-string"
 f"Second part of f-string")
  • Create a multiple string with \ separator after f-string.
    Example:
f"Some_part" \
f"Another part"
誰ツ都不明白 2025-01-20 07:13:01

是的,利用隐式字符串连接,您可以这样做:

if __name__ == "__main__":
    x_true = 3
    x_prov = 4

    assert x_prov == x_true, (
        f"The provided value for x, namely {x_prov}, is invalid,"
        f" the true value should be {x_true}!"
    )

Yes, making use of implicit string concatenation, you can do this:

if __name__ == "__main__":
    x_true = 3
    x_prov = 4

    assert x_prov == x_true, (
        f"The provided value for x, namely {x_prov}, is invalid,"
        f" the true value should be {x_true}!"
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文