Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
continuing an f-string
#1
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
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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:
Skaperen likes this post
Reply
#3
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
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020