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 of the specified width where the original string is right-aligned and the left side is padded with zeros (0) instead of spaces or other characters.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ string.html
string โ€” Common string operations
The format_spec field contains a specification of how the value should be presented, including such details as field width, alignment, padding, decimal precision and so on.
๐ŸŒ
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")
๐ŸŒ
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
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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....
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Right-justify, Center, Left-justify Strings and Numbers in Python | note.nkmk.me
May 18, 2023 - Use the rjust(), center(), or ljust() methods to right-justify, center, or left-justify strings str in Python. You can convert numbers (int and float) to strings using str() and apply these methods. R ...
๐ŸŒ
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 - < indicates left-align, ^ is for center-align, and > is for right-align. The number represents the total width of the field. The result is neatly formatted text: ... You can easily control the precision of numbers displayed in your strings using ...
๐ŸŒ
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.
๐ŸŒ
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....
๐ŸŒ
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 โ€บ 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.