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
🌐
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" >>> ...
Discussions

python - How to right-align numeric data? - Stack Overflow
I have some data that I am displaying in 3 column format, of the form: key: value key: key: value key: value. Here's an example: p: 1 sl: 10 afy: 4 q: 12 lg: 10 k... More on stackoverflow.com
🌐 stackoverflow.com
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 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
render_template table header styling - always align: right
Not an answer to your question but I have given up df.to_html unless I just want to see data real quick. Now I just loop through the df instead and can apply the styles I need without having to fight for hours with pandas for applying even the simplest formatting options, such as aligning text. More on reddit.com
🌐 r/flask
3
2
January 10, 2022
🌐
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.
🌐
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.
🌐
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).
🌐
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 ...
🌐
Medium
medium.com › @pivajr › pythonic-tips-how-to-align-strings-to-the-left-right-and-center-in-python-557db734cce6
Pythonic Tips: How to Align Strings to the Left, Right, and Center in Python | by Dilermando Piva Junior | Medium
May 22, 2025 - Here, ljust(20) ensures the product name is left-aligned within a 20-character column, while rjust(20) pushes monetary values to the right, creating a table-like illusion in terminal interfaces. The center() method is perfect for headers and visual dividers. Introduced in Python 3.6, f-strings bring poetic elegance to text formatting.
Find elsewhere
🌐
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....
🌐
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 ...
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › how-to-align-text-strings-using-python
How to Align Text Strings using Python?
# 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")
🌐
Scaler
scaler.com › home › topics › python › string formatting in python
String Formatting in Python - Scaler Topics
June 11, 2024 - Numbers can also be left, right, or center aligned using Python format() method.
🌐
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 ...
🌐
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.
🌐
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.
🌐
w3resource
w3resource.com › python-exercises › string › python-data-type-string-exercise-37.php
Python: Display a number in left, right and center aligned of width 10 - w3resource
June 12, 2025 - Write a Python program to use f-string formatting to align a number in a field of width 10 in different ways. Write a Python program to print a number in left, right, and center alignments using the format() method with appropriate specifiers.
🌐
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

🌐
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