简体   繁体   English

Python 中的多行 f 字符串

[英]Multiline f-string in Python

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. - self.text 点附近的细实线是 80 个字符标记。

I'm trying to split it into different lines in the most pythonic way 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,有没有办法使字符串正确且代码兼容?

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

I think it would be我想这会是

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

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”,因为您使用的是多行字符串。

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

You can mix the multiline quoting styles and regular strings and f-strings:您可以混合使用多行引用样式和常规字符串和 f 字符串:

foo = 'bar'
baz = 'bletch'
print(f'foo is {foo}!\n',
      'bar is bar!\n',
      f"baz is {baz}!\n",
      '''bletch
      is
      bletch!''')

Prints this (note the indentation):打印这个(注意缩进):

foo is bar!
 bar is bar!
 baz is bletch!
 bletch
      is
      bletch!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM