Multiline f-string in Python

PythonStringPython 3.6F String

Python Problem Overview


I'm trying to write PEP-8 compliant code for a domestic project and I have a line with an f-string that is more than 80 characters long

- the solid thin line near the dot at self.text is the 80 char mark.

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

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?

Python Solutions


Solution 1 - Python

From Style Guide for Python Code:

> The preferred way of wrapping long lines is by using Python's 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'
    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().

Solution 2 - Python

I think it would be

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

Solution 3 - 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 don't need to use "\n" because you are using a multiple-line string.

Solution 4 - Python

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

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

Solution 5 - Python

You can mix the multiline quoting styles and regular strings and f-strings:

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOwlzyView Question on Stackoverflow
Solution 1 - PythonnoddyView Answer on Stackoverflow
Solution 2 - PythonJoran BeasleyView Answer on Stackoverflow
Solution 3 - PythonlmiguelvargasfView Answer on Stackoverflow
Solution 4 - PythoncodarriorView Answer on Stackoverflow
Solution 5 - PythonTimur ShtatlandView Answer on Stackoverflow