Try this approach using the newer str.format syntax. This uses a width of 12, space padded, right aligned.
line_new = '{:>12} {:>12} {:>12}'.format(words[0], words[1], words[2])
Example with the interpreter:
>>> line = "123 456 789"
>>> words = line.split(' ')
>>> line_new = '{:>12} {:>12} {:>12}'.format(words[0], words[1], words[2])
>>> print(line_new)
123 456 789
And here's how to do it using the old % syntax (useful for older versions of Python that don't support str.format):
line_new = '%12s %12s %12s' % (words[0], words[1], words[2])
Answer from Mark Byers on Stack Overflowpython - Format output string, right alignment - Stack Overflow
How do I right-justify strings with dollar signs and percentages?
Left-justifying floating-point number output
Formatting text to be justified in Python 3.3 with .format() method - Stack Overflow
Try this approach using the newer str.format syntax. This uses a width of 12, space padded, right aligned.
line_new = '{:>12} {:>12} {:>12}'.format(words[0], words[1], words[2])
Example with the interpreter:
>>> line = "123 456 789"
>>> words = line.split(' ')
>>> line_new = '{:>12} {:>12} {:>12}'.format(words[0], words[1], words[2])
>>> print(line_new)
123 456 789
And here's how to do it using the old % syntax (useful for older versions of Python that don't support str.format):
line_new = '%12s %12s %12s' % (words[0], words[1], words[2])
Here is another way how you can format using 'f-string' format:
print(
f"{'Trades:':<15}{cnt:>10}",
f"{'Wins:':<15}{wins:>10}",
f"{'Losses:':<15}{losses:>10}",
f"{'Breakeven:':<15}{evens:>10}",
f"{'Win/Loss Ratio:':<15}{win_r:>10}",
f"{'Mean Win:':<15}{mean_w:>10}",
f"{'Mean Loss:':<15}{mean_l:>10}",
f"{'Mean:':<15}{mean_trd:>10}",
f"{'Std Dev:':<15}{sd:>10}",
f"{'Max Loss:':<15}{max_l:>10}",
f"{'Max Win:':<15}{max_w:>10}",
f"{'Sharpe Ratio:':<15}{sharpe_r:>10}",
sep="\n"
)
This will provide the following output:
Trades: 2304
Wins: 1232
Losses: 1035
Breakeven: 37
Win/Loss Ratio: 1.19
Mean Win: 0.381
Mean Loss: -0.395
Mean: 0.026
Std Dev: 0.56
Max Loss: -3.406
Max Win: 4.09
Sharpe Ratio: 0.7395
What you are doing here is you are saying that the first column is 15 chars long and it's left-justified and the second column (values) is 10 chars long and it's right-justified.
If you joining items from the list and you want to format space between items you can use `` and regular formatting techniques.
This example separates each number by 3 spaces. The key here is f"{'':>3}"
print(f"{'':>3}".join(str(i) for i in range(1, 11)))
output:
1 2 3 4 5 6 7 8 9 10
I have an intro Python assignment that asks me to list out a series of numbers with two decimal places and align all the decimal points.
Someone please tell me how I can line these numbers up. Formatting doesn't seem to do literally anything at all.
The amount can be formated like this:
"${:.2f}".format(amount)
You can add padding to a string, for example for a width of 20:
"{:20s}".format(mystring)
You can right align the string, for example with a width of 7:
"{:>7s}".format(mystring)
Putting all this together:
s = "The subtotal was:"
a = 24.95
print("{:20s}{:>7s}".format(s, "${.2f}".format(a))
If you know the maximum sizes of the text and numbers, you can do
val_str = '${:.2f}'.format(val)
print('{:<18} {:>6}'.format(name+':', val_str))
It gets trickier if these aren't known in advance. Here's an approach, assuming names and values are lists:
value_format = '${:.2f}'.format
name_format = '{}:'.format
values_fmt = [value_format(val) for val in values]
names_fmt = [name_format(name) for name in names]
max_value_len = max(len(x) for x in values_fmt)
max_name_len = max(len(x) for x in names_fmt)
for name, val in zip(names_fmt, values_fmt):
print('{:<{namelen}} {:>{vallen}}'.format(name, val,
namelen=max_name_len, vallen=max_value_len))
You can prefix the size requirement with - to left-justify:
sys.stdout.write("%-6s %-50s %-25s\n" % (code, name, industry))
This version uses the str.format method.
Python 2.7 and newer
sys.stdout.write("{:<7}{:<51}{:<25}\n".format(code, name, industry))
Python 2.6 version
sys.stdout.write("{0:<7}{1:<51}{2:<25}\n".format(code, name, industry))
UPDATE
Previously there was a statement in the docs about the % operator being removed from the language in the future. This statement has been removed from the docs.