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 OverflowTry 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
How do I align/move text right?
How to align text centered on a character?
How do I right-justify strings with dollar signs and percentages?
Help with formatting table cells using python-docx
If anyone comes across this looking for the same type of thing, I've figured out that you have to add runs to what you want to add to the cell. For example, if you want the text to be bold the code looks like this:
test_table = doc.add_table(rows=1, cols=2)
test_table.style = "Table Grid"
hdr_cells = test_table.rows[0].cells
paragraph = hdr_cells[0].paragraphs[0]
run = paragraph.add_run('This is some text')
run.bold = TrueThis stackoverflow post was helpful https://stackoverflow.com/questions/57364540/with-python-docx-when-i-change-font-properties-in-a-table-cell-the-whole-docum
More on reddit.comHi all! So I’ve been trying to learn python by giving myself small tasks to do and I decided I wanted to make a right aligned right triangle (I know it’s really specific) but it never works, I’ve tried a few different things but I am never able to align it to the right, it always aligns left I have everything else correct though
*
**
***
****
*****Is what it’s supposed to look like
for a in range (-1, -6, -1):
for b in range (a, 0):
print ("*",end="")
print ("")There is my code, if someone could guide me on what I’m doing wrong/ how to fix it I’d be super grateful!
Sorry for formatting, am on mobile
Hi guys,
Can anyone help me on how I can align text in a string so it is centered on the ": "?
I was thinking something like string concatenation but not sure how to align it so I would not be blocked by a character limit on the left side.
I need to create a function that outputs the following:
Toyota Prius Current: 12500km Last service: 11000km Next service: 21000km Service due: False Toyota Prius Current: 12500km Last service: 11000km Next service: 21000km Service due: False