Multiline f-string in Python

Multiline f-string in Python

From Style Guide for Python Code:

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

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

return (
    f{self.date} - {self.time}n
    fTags: {self.tags}n
    fText: {self.text}
)

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

I think it would be

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

Multiline f-string in Python

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

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 dont need to use n because you are using a multiple-line string.

Leave a Reply

Your email address will not be published. Required fields are marked *