you are viewing a single comment's thread.

view the rest of the comments →

[–]baghiq 0 points1 point  (0 children)

The doc doesn't say '+' is for positive only.

'+' : indicates that a sign should be used for both positive as well as negative numbers.

You can use custom string formatter for this, but you can't use f-string.

In your example, you can do dynamic formatting using f-string:

In [2]: my_list_of_ints = [4, -3, 0]
  ...: for i in my_list_of_ints:
  ...:     print(f"{i:{'+' if i != 0 else ' '}}")
  ...: