Python Forum
continuing an f-string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: continuing an f-string (/thread-35182.html)



continuing an f-string - Skaperen - Oct-07-2021

i just discovered that when continuing an f-string, what i quoted next needs to also be an f-string if it has any formatting elements that need to be handled like they are in an f-string.
abc = 'woot'
ghi = 'xyzzy'
xyz = f'foobar'\
      f' {abc} {ghi}'
print(xyz)
Output:
foobar woot xyzzy



RE: continuing an f-string - Yoriz - Oct-07-2021

I prefere using () then \
foo = "foo"
abc = "woot"
ghi = "xyzzy"
xyz = (f"{foo}bar"
      f" {abc} {ghi}")
print(xyz)
Output:
foobar woot xyzzy

It is also pep8's preferred way.
https://www.python.org/dev/peps/pep-0008/ Wrote:The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

Backslashes may still be appropriate at times. For example, long, multiple with-statements cannot use implicit continuation, so backslashes are acceptable:



RE: continuing an f-string - Skaperen - Oct-08-2021

i thought that only worked where the lines to be continued ended in a comma. nice to know that this is not the case. i'll probably spend the weekend removing \ in past code Dance