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 Overflow
Top answer
1 of 8
337

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])
2 of 8
90

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
🌐
GeeksforGeeks
geeksforgeeks.org › python › string-alignment-in-python-f-string
String Alignment in Python f-string - GeeksforGeeks
July 15, 2025 - Python’s f-strings allow text alignment using three primary alignment specifiers: < (Left Alignment): Aligns the text to the left within the specified width. > (Right Alignment): Aligns the text to the right within the specified width.
Discussions

How do I align/move text right?
Spaces = 12 For i in range(1,13): Print(" " * (spaces-i) + "*" * i) More on reddit.com
🌐 r/learnpython
13
3
June 9, 2020
How to align text centered on a character?
You can use the string formatting mini-language in an f-string or .format() string method. It's possible to left or right justify values in a given field width, along with many other neat manipulations. Here's a simple example showing how, try running it: # make up some dummy data: (label, value) data = (("longer name", 42), ("short", 3.14159), ("really really long", -5.1), ) label_width = 20 # must be at least width of longest label for (label, value) in data: print(f"{label:>{label_width}s}: {value}") Edit: grammar. More on reddit.com
🌐 r/learnpython
6
5
August 1, 2024
How do I right-justify strings with dollar signs and percentages?
"Formatting doesn't seem to do literally anything at all." Well, all of Python's number formatting methods allow you to restrict the output to two decimal places and to right justify it. I suggest that you post your code so that people here can advise you what is going wrong with it. You can edit your original post to include it. Guide to posting code in this subreddit: https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F More on reddit.com
🌐 r/learnpython
8
3
February 13, 2022
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 = True

This 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.com
🌐 r/learnpython
1
1
February 6, 2021
🌐
Stack Abuse
stackabuse.com › formatting-strings-with-python
Formatting Strings with Python
July 17, 2023 - Both the str.format() and interpolation ... and formatting specifications to the right. You can align values within a specified length of text by using the <, >, or ^ symbols to specify left align, right align, or centering, respectively....
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-align-text-strings-using-python
How to Align Text Strings using Python?
March 26, 2026 - # Left align with 30 character width text = "Left Aligned String!" print(f"{text:<30}") print(f"{'Hello':<20}World") ... # Right align with 30 character width text = "Right Aligned String!" print(f"{text:>30}") print(f"{'Hello':>20}World")
🌐
C# Corner
c-sharpcorner.com › article › ways-to-align-text-strings-in-python
Ways To Align Text Strings In Python
September 13, 2022 - The alternate way of doing this is by using format(…) function as shown below: my_text = “Shift me.” print(format(my_text,’>25')) ... For right-aligned text, you can use rjust(…) function, which stands for right justification.
🌐
Towards Data Science
towardsdatascience.com › home › programming › formatting strings and numbers in python
Formatting strings and numbers in python | Towards Data Science
June 27, 2021 - You can use the :>, :< or :^ option in the f-format to left align, right align or center align the text that you want to format. ... After formatting the alignment of output text ...
🌐
Reddit
reddit.com › r/learnpython › how do i align/move text right?
r/learnpython on Reddit: How do I align/move text right?
June 9, 2020 -

Hi 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

Find elsewhere
🌐
Medium
shweta-lodha.medium.com › ways-to-align-text-strings-in-python-ef0f1dab5a28
Ways To Align Text Strings In Python | by Shweta Lodha | Medium
August 13, 2022 - ... The above lines will display left-aligned text with a length of 25. The alternate way of doing this is by using format(…) function as shown below: ... For right-aligned text, you can use rjust(…) function, which stands for right ...
🌐
Note.nkmk.me
note.nkmk.me › home › python
Right-justify, Center, Left-justify Strings and Numbers in Python | note.nkmk.me
May 18, 2023 - Format strings and numbers with format() in Python · To right-justify, center, or left-justify, use [CHARACTER][DIRECTION][STRING_LENGTH] as the format string.
🌐
Textual
textual.textualize.io › styles › text_align
Textual - Text-align
# Set text in the widget to be right aligned widget.styles.text_align = "right"
🌐
YouTube
youtube.com › watch
#67 Python for Beginners: Ways To Align Text In Python - YouTube
Blog: http://www.shwetalodha.in/Medium: https://medium.com/@shweta-lodhaJoin Medium membership at just $5/month :https://shweta-lodha.medium.com/membershipWa
Published: August 13, 2022
🌐
Educative
educative.io › answers › how-to-format-strings-in-python
How to format strings in Python
> : right-align text in the field · The general syntax is: {start:align_type end} .format(value) Here start and end define the positional index of the value to start and end the padding and align_type can be <, ^, >. # To demonstrate aligning of spaces · # center align Python and left align 1989 ·
🌐
LabEx
labex.io › tutorials › python-how-to-align-output-in-python-printing-418802
How to align output in Python printing | LabEx
The script you just created demonstrates three basic string alignment methods in Python: ljust(width): Left-justifies the string within a field of the specified width. rjust(width): Right-justifies the string within a field of the specified width. ...
🌐
Sling Academy
slingacademy.com › article › python-how-to-algin-a-string
Python: How to Algin a String (Left, Right, and Center) - Sling Academy
text = "Tortoise" # Left alignment ... aligned string with a pair of single quotes to make the spaces visible. 2. Right alignment: Use the > character followed by the desired width:...
🌐
Processing
py.processing.org › reference › textalign
textAlign() \ Language (API)
Python Mode for Processing extends the Processing Development Environment with the Python programming language.
🌐
Matplotlib
matplotlib.org › stable › gallery › text_labels_and_annotations › text_alignment.html
Text alignment — Matplotlib 3.11.2 documentation
import matplotlib.pyplot as plt fig, ax = plt.subplots() # Build a rectangle in axes coords left, width = .25, .5 bottom, height = .25, .5 right = left + width top = bottom + height p = plt.Rectangle((left, bottom), width, height, fill=False) p.set_transform(ax.transAxes) p.set_clip_on(False) ax.add_patch(p) ax.text(left, bottom, 'left top', horizontalalignment='left', verticalalignment='top', transform=ax.transAxes) ax.text(left, bottom, 'left bottom', horizontalalignment='left', verticalalignment='bottom', transform=ax.transAxes) ax.text(right, top, 'right bottom', horizontalalignment='right
🌐
Programiz
programiz.com › python-programming › methods › string › rjust
Python String rjust() (With Examples)
Online Python Online JavaScript ... Online Go Online Rust Online Scala Online Dart Online R Online Ruby ... The rjust() method right aligns the string up to a given width using a specified character....
🌐
Reddit
reddit.com › r/learnpython › how to align text centered on a character?
r/learnpython on Reddit: How to align text centered on a character?
August 1, 2024 -

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
🌐
Scaler
scaler.com › home › topics › python › string formatting in python
String Formatting in Python - Scaler Topics
June 11, 2024 - ... For the alignment of strings, ... Here n is the total length of the required output string. ... To right align a string, we use the “:>n” symbol inside the placeholder....