python - 符合 PEP8 的长 f 字符串拆分

标签 python split pep8 f-string

如何将下面的长行分成两行,以符合PEP8?

percentage = f"{state[0] / state[1] * 100:{3 + (decimals > 0) + decimals}.{decimals}f}%"

注意: 这里的 f-string 不能简单地分成两个 f-strings,如对 previously asked question 的已接受答案中所建议的那样,因为这会破坏格式。因此,这里的这个问题需要一个不同的、更通用的解决方案。

最佳答案

不要害怕使用变量!这将使您的代码更具可读性。

a = state[0] / state[1] * 100
b = 3 + (decimals > 0) + decimals
# of course, you would change the names here
percentage = f"{a:{b}.{decimals}f}%"

关于python - 符合 PEP8 的长 f 字符串拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61187318/

相关文章:

c# - 如何使用字符串定界符拆分字符串?

python - 在 Keras 中实现自定义损失函数

Python 分割字符串忽略\"

python - pycurl,如何为多选表单发送POST数据?

google-apps-script - 将多行单元格拆分为不同的行

python - Auto-PEP8通过将lambda转换为def函数来添加行,如何禁用此特定的自动格式?

python - 如何让 PyC​​harm 的填充段落命令覆盖默认边距?

Python:Pep8 E128 缩进错误......这怎么能被样式化?

python - SQLAlchemy:过滤多对多但返回所有结果

python - 如何解决 TensorFlow 中的 'BiasGrad requires tensor size <= int32 max' InvalidArgumentError?