3

How to split the following long line into two lines, in order to conform to PEP8?

percentage = f"{state[0] / state[1] * 100:{3 + (decimals > 0) + decimals}.{decimals}f}%"

Note: The f-string here cannot simply be split into two f-strings as suggested in the accepted answer to a previously asked question, since this would break formatting. This question here hence needs a different and more general solution.

Florian Krause
  • 183
  • 1
  • 2
  • 8
  • Just add a new line after "=" -> `percentage = \ f"{state[0] / state[1] * 100:{3 + (decimals > 0) + decimals}.{decimals}f}"` – Ilya Maier Apr 13 '20 at 11:55
  • 3
    If you're that worried about PEP8 on a 88 character line, simply compute whatever's inside your f-string in the line above. And remember that [a foolish consistency is the hobgoblin of little minds](https://www.python.org/dev/peps/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds). – jfaccioni Apr 13 '20 at 11:57

1 Answers1

2

Don't be afraid of using variables! It will make your code more readable.

a = state[0] / state[1] * 100
b = 3 + (decimals > 0) + decimals
# of course, you would change the names here
percentage = f"{a:{b}.{decimals}f}%"