I want to format array of numbers with same width using f-strings. Numbers can be both positive or negative.
Minimum working example
import numpy as np
arr = np.random.rand(10) - 0.5
for num in arr:
print(f"{num:0.4f}")
The result is
0.0647
-0.2608
-0.2724
0.2642
0.0429
0.1461
-0.3285
-0.3914
Due to the negative sign, the numbers are not printed off with the same width, which is annoying. How can I get the same width using f-strings?
One way that I can think of is converting number to strings and print string. But is there a better way than that?
for num in a:
str_ = f"{num:0.4f}"
print(f"{str_:>10}")