Python 中的多行 f 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45965007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:23:26  来源:igfitidea点击:

Multiline f-string in Python

pythonstringpython-3.6f-string

提问by Owlzy

I'm trying to write PEP-8 compliant code for a domestic project (I must admit that those are my first steps in the python world) and i've got a f-string that is more than 80 char long

我正在尝试为国内项目编写符合 PEP-8 的代码(我必须承认这是我在 Python 世界中的第一步)并且我有一个超过 80 个字符的 f 字符串

- the solid thin line near the dot at self.text is the 80 char mark. (Sorry for the sad link without preview but i must have 10+ rep to post 'em)

- self.text 点附近的细实线是 80 个字符标记。(抱歉没有预览的悲伤链接,但我必须有 10 多个代表才能发布它们)

I'm trying to split it into different lines in the most pythonicway but the only aswer that actually works is an error for my linter

我试图把它分成不同的线路在最Python的方式,但唯一的aswer,实际工作是我的棉短绒错误

Working Code:

工作代码:

def __str__(self):
    return f'{self.date} - {self.time},\nTags:' + \
    f' {self.tags},\nText: {self.text}'

Output:

输出:

2017-08-30 - 17:58:08.307055,
Tags: test tag,
Text: test text

The linter thinks that i'm not respecting E122 from PEP-8, is there a way to get the string right and the code compliant?

linter 认为我不尊重 PEP-8 中的 E122,有没有办法使字符串正确且代码兼容?

采纳答案by noddy

From Style Guide for Python Code:

来自Python 代码风格指南

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces.

包装长行的首选方法是在括号、方括号和大括号内使用 Python 的隐含行续行。

Given this, the following would solve your problem in a PEP-8 compliant way.

鉴于此,以下内容将以符合 PEP-8 的方式解决您的问题。

return (
    f'{self.date} - {self.time}\n'
    f'Tags: {self.tags}\n'
    f'Text: {self.text}'
)

Python strings will automatically concatenate when not separated by a comma, so you do not need to explicitly call join().

Python 字符串将在未用逗号分隔时自动连接,因此您无需显式调用join().

回答by Joran Beasley

I think it would be

我想这会是

return f'''{self.date} - {self.time},
Tags: {self.tags},
Text: {self.text}'''

回答by lmiguelvargasf

You can use either triple single quotation marks or triple double quotation marks, but put an f at the beginning of the string:

您可以使用三重单引号或三重双引号,但在字符串的开头放置一个 f:

Triple Single Quotes

三重单引号

return f'''{self.date} - {self.time},
Tags:' {self.tags},
Text: {self.text}'''

Triple Double Quotes

三重双引号

return f"""{self.date} - {self.time},
Tags:' {self.tags},
Text: {self.text}"""

Notice that you don't need to use "\n" because you are using a multiple-line string.

请注意,您不需要使用“\n”,因为您使用的是多行字符串。

回答by codarrior

As mentioned by @noddy, the approach also works for variable assignment expression:

正如@noddy 所提到的,该方法也适用于变量赋值表达式:

var1 = "foo"
var2 = "bar"
concat_var = (f"First var is: {var1}"
              f" and in same line Second var is: {var2}")
print(concat_var)

should give you:

应该给你:

First var is: foo and in same line Second var is: bar