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
🌐
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

Discussions

python - How to right-align numeric data? - Stack Overflow
Mea culpa. I found this question because I was trying to right-align strings, and I didn't read the OP question close enough to catch that only numbers were being printed. Thanks for clarifying. 2012-09-29T05:51:00.277Z+00:00 ... In Python 2.5 use rjust (on strings). More on stackoverflow.com
🌐 stackoverflow.com
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
How to right-align text?
Use topright. You also would want a label class over a drawText function. You do not want to waste cpu cycle constantly render text. That very costly. Example class Label: def __init__(self, font, text, color, position, anchor="topleft"): self.image = font.render(text, 1, color) self.rect = self.image.get_rect() setattr(self.rect, anchor, position) print(self.rect) def draw(self, surface): surface.blit(self.image, self.rect) More on reddit.com
🌐 r/pygame
4
3
February 19, 2021
How do I right-justify the column headers in display? (Notebook)
For an individual DataFrame: df.style.set_table_styles([ dict(selector='td', props=[('text-align', 'right')]), ]) Most of the methods on a Styler object (what df.style returns) return a modified Styler object, so you can chain them together: df.style.set_table_styles([ dict(selector='td', props=[('text-align', 'right')]), ]).format('{:.2f}').bar(color='#AAAAAA') More on reddit.com
🌐 r/IPython
4
2
September 7, 2016
🌐
Note.nkmk.me
note.nkmk.me › home › python
Right-justify, Center, Left-justify Strings and Numbers in Python | note.nkmk.me
May 18, 2023 - Built-in Types - str.ljust() — Python 3.11.3 documentation · Its usage is the same as rjust() and center(). Although it may not be visible in the output, trailing spaces are added when the second argument is omitted. s = 'abc' print(s.ljust(8)) # abc print(s.ljust(8, '*')) # abc***** ... If you want to right-justify, center, or left-justify a non-string object such as an integer (int) or floating point number (float), convert it to a string with str() and call these methods.
🌐
GeeksforGeeks
geeksforgeeks.org › string-alignment-in-python-f-string
String Alignment in Python f-string - GeeksforGeeks
March 22, 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.
🌐
Stack Abuse
stackabuse.com › formatting-strings-with-python
Formatting Strings with Python
July 17, 2023 - You can align values within a specified length of text by using the <, >, or ^ symbols to specify left align, right align, or centering, respectively. Then you follow those symbols with a character width you desire. Python > 2.6: >>> left_aligned = "Left Align" >>> center = "Centered" >>> ...
Find elsewhere
🌐
Programiz
programiz.com › python-programming › methods › string › rjust
Python String rjust() (With Examples)
# right aligns 'Python' up to width ... equal to the length of the string, the original string is returned. ... In the above example, we have used the rjust() method to right justify the text string....
🌐
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.
🌐
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 ...
🌐
Index.dev
index.dev › blog › right-justify-objects-python-graphics
How to Right-Justify Objects in Python Graphics Using Popular Libraries
September 10, 2024 - Usually starting with a window, you build a GUI using Tkinter by adding widgets—such as labels, buttons, and text fields—then running the main event loop. Using the anchor and justify options in Tkinter lets you right-align buttons and labels.
🌐
Educative
educative.io › answers › how-to-format-strings-in-python
How to format strings in Python
Any value (integer, number, string or character) can be padded from left and right. ... Here start and end define the positional index of the value to begin and finish the padding. ... Here the value is 40. The padding will start at 4. It will count 4 as 0, 0 as 1 and then prints 3 spaces and the remaining string. print("{0:4}, is the computer science portal for {1:10}!" ... By default, strings are left-aligned within the field, and numbers are right-aligned.
🌐
GeeksforGeeks
geeksforgeeks.org › python-string-ljust-rjust-center
Python String - ljust(), rjust(), center() - GeeksforGeeks
January 2, 2025 - The rjust() method in Python is a string method used to right-align a string within a specified width by padding it with a specified character (default is a space).
🌐
Vultr Docs
docs.vultr.com › python › standard-library › str › rjust
Python str rjust() - Right-Justify String | Vultr Docs
December 31, 2024 - The str.rjust() method in Python is a built-in String method used for right-justifying string data. This function is typically used to align text to the right side of a specified width by padding it on the left with a specified fill character ...
🌐
USAVPS
usavps.com › home › blog › python tutorial: how to right align numbers in python?
Python Tutorial: How to Right Align Numbers in Python? - USAVPS
March 18, 2026 - Python is a versatile programming language that offers various ways to format strings, including aligning text and numbers. Right aligning numbers can be particularly useful when displaying tabular data or creating reports where readability is essential.
🌐
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 ...
🌐
Iditect
iditect.com › faq › python › format-output-string-right-alignment-in-python.html
Format output string, right alignment in python
You can format output strings with right alignment in Python using the .format() method or f-strings (available in Python 3.6 and later) with the > (greater than) symbol. Here's how to do it with both methods:
🌐
W3Schools
w3schools.com › python › ref_string_rjust.asp
Python String rjust() Method
Note: In the result, there are actually 14 whitespaces to the left of the word banana. The rjust() method will right align the string, using a specified character (space is default) as the fill character.