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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ string-alignment-in-python-f-string
String Alignment in Python f-string - GeeksforGeeks
July 15, 2025 - Explanation: max_width dynamically calculates the longest word's length plus 2 spaces for padding, ensuring left-aligned, consistently spaced and readable output. ... Explanation: The first print statement centers "Python" within 20 spaces, filling the empty space with -. The second line left-aligns "Python" within 15 spaces, filling the remaining space with *. The third line right-aligns "Python" within 15 spaces, filling the left side with =.
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
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-manipulate-python-string-alignment-419448
How to manipulate Python string alignment | LabEx
String alignment is a fundamental technique in Python for formatting and presenting text in a structured and visually appealing manner.
๐ŸŒ
Shikshasanchar
shikshasanchar.com โ€บ courses โ€บ python โ€บ strings โ€บ alignment-methods
String Alignment methods in Python - ShikshaSanchar
Returns a new string that is center-aligned in a string of specified width. Extra space is filled with optional characters like *, -, + etc (default is space). ... Width = 11 means the total string length becomes 11. Original string "Python" has 6 characters, so 5 extra spaces are added (or ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-align-text-strings-using-python
How to Align Text Strings using Python?
In this article, we will learn how to align text strings using Python. Python provides multiple ways to align text: f-strings with alignment specifiers and built-in string methods like ljust(), rjust(), and center().
๐ŸŒ
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.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ string.html
string โ€” Common string operations
July 4, 2010 - This is equivalent to a fill character of '0' with an alignment type of '='.
Find elsewhere
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ formatting-strings-with-python
Formatting Strings with Python
July 17, 2023 - In Python there happens to be multiple techniques for formatting strings, four to be exact. Interestingly this kinda goes against the grain of the Python mindset that there should generally be one clear best way to accomplish a task.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ python โ€บ string formatting in python
String Formatting in Python - Scaler Topics
June 11, 2024 - We can also perform different operations like truncating, padding, and alignment on the strings in Python using the format() method.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_center.asp
Python String center() Method
Remove List Duplicates Reverse ... Interview Q&A Python Training ... The center() method will center align the string, using a specified character (space is default) as the fill character....
๐ŸŒ
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).
๐ŸŒ
pythontutorials
pythontutorials.net โ€บ blog โ€บ python-spacing-and-aligning-strings
Python String Alignment Guide: How to Space and Align Text Columns Without Manual Spaces (Fixing rjust/ljust Issues) โ€” pythontutorials.net
F-strings: Best for most alignment tasks (readability, dynamic widths, mixed alignment). format() method: Use when f-strings arenโ€™t available (e.g., Python <3.6) or for dynamic format strings.
๐ŸŒ
Medium
thetechplatform.medium.com โ€บ fstring-in-python-the-complete-guide-to-fstring-formatting-900676a768d5
Fstring in Python: The Complete Guide to Fstring Formatting | by The Tech Platform | Medium
February 12, 2024 - You can use various symbols to specify string alignment and padding within curly braces {}. These symbols are combined with a number to control the width and alignment of the formatted string.
๐ŸŒ
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 - It overcomes the limitation that ... the 'order-problem' 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....
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ standard library โ€บ str โ€บ center()
Python str center() - Center Align String
December 25, 2024 - Specify a custom character for filling the extra space. Apply the center() method with both width and the fillchar parameter. ... In this example, the word "hello" is centered within a 20-character wide string, using '-' as the fill character.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-align-output-in-python-printing-418802
How to align output in Python printing | LabEx
Let's create a simple Python script to demonstrate the basic alignment techniques. Open the WebIDE in your LabEx environment. Create a new file named basic_alignment.py in the /home/labex/project directory by clicking on the "New File" icon in the WebIDE. ... ## basic_alignment.py print("Basic String Alignment Examples") print("-" * 30) ## Left alignment example print("Left alignment:") print("Python".ljust(20) + "|") print("Programming".ljust(20) + "|") print("Alignment".ljust(20) + "|") print() ## Right alignment example print("Right alignment:") print("Python".rjust(20) + "|") print("Programming".rjust(20) + "|") print("Alignment".rjust(20) + "|") print() ## Center alignment example print("Center alignment:") print("Python".center(20) + "|") print("Programming".center(20) + "|") print("Alignment".center(20) + "|")