You can prefix the size requirement with - to left-justify:

sys.stdout.write("%-6s %-50s %-25s\n" % (code, name, industry))
Answer from Matthew Trevor on Stack Overflow
🌐
Note.nkmk.me
note.nkmk.me › home › python
Right-justify, Center, Left-justify Strings and Numbers in Python | note.nkmk.me
May 18, 2023 - Left-justify strings: ljust() Apply to numbers (int and float) Format strings: format(), f-strings · Use the rjust() method to right-justify strings. Built-in Types - str.rjust() — Python 3.11.3 documentation · The first argument determines the length of the output string.
🌐
Educative
educative.io › answers › how-to-format-strings-in-python
How to format strings in Python
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 ...
🌐
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › string-alignment-in-python-f-string
String Alignment in Python f-string - GeeksforGeeks
July 15, 2025 - Python’s f-strings make it easy ... text alignment using three primary alignment specifiers: < (Left Alignment): Aligns the text to the left within the specified width....
🌐
Scaler
scaler.com › home › topics › python › string formatting in python
String Formatting in Python - Scaler Topics
June 11, 2024 - We can also perform different ... we use the same syntax we used for the alignment of numbers. To left-align a string, we use the “:<n” symbol inside the placeholder....
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
Find elsewhere
🌐
C# Corner
c-sharpcorner.com › article › ways-to-align-text-strings-in-python
Ways To Align Text Strings In Python
September 13, 2022 - Let’s have a look at those: ... For left-aligned text, you can use ljust(…) function, which stands for left justification. my_text = “Shift me.” print(my_text.ljust(25) ) The above lines will display left-aligned text with a length of 25.
🌐
tutorialpedia
tutorialpedia.org › blog › str-format-how-to-left-justify
How to Left-Justify Strings in Python Using str.format(): A Complete Guide — tutorialpedia.org
Left-justification—aligning text to the left margin with padding on the right—is one of the most common alignment techniques. In Python, the str.format() method is a powerful tool for string formatting, and it includes built-in support for left-justification.
🌐
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.
🌐
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 display a number in left, right, and center aligned with a width of 10. ... # Define a variable 'x' and assign it the value 22 (an integer). x = 22 # Print an empty line for spacing. print() # Print the original value of 'x' with a label. print("Original Number: ", x) # Format and print the value of 'x' with left alignment, a width of 10 characters, and padding using spaces.
🌐
Sling Academy
slingacademy.com › article › python-how-to-algin-a-string
Python: How to Algin a String (Left, Right, and Center) - Sling Academy
They are useful for formatting strings in a fixed-width format or aligning text in tabular structures. 1. ljust(width[, fillchar]): Left-aligns the string by adding spaces or a specified fill character (fillchar) to the right of the string until it reaches the specified width.
🌐
Devgex
devgex.com › en › article › 00047227
String Formatting in Python: Multiple Approaches for Left-Aligned Fixed-Width Text - DevGex
December 7, 2025 - Python's earliest string formatting method uses the % operator, with syntax borrowed from C's printf function. To achieve left alignment, simply add a minus sign '-' prefix before the width specifier:
🌐
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...
🌐
GoLinuxCloud
golinuxcloud.com › home › programming › python string format
Python String Format: f-strings, format(), %, Padding, and Precision
June 23, 2026 - String formatting inserts values into a string. It can also control width, alignment, padding, decimal places, signs, commas, percentages, and dates. For joining plain text without format specifiers, see Python concatenate strings. ... Use f-strings for most new Python 3.6+ code.
🌐
Stack Overflow
stackoverflow.com › questions › 51684216 › align-left-or-right-using-a-variable-in-format
python 3.x - Align left or right using a variable in .format() - Stack Overflow
August 4, 2018 - Make a variable, item, that points to a string, "car". Make a variable, cost, that points to 13499.99. Print out a line that has item in a left-justified area of 10 characters, and cost in a right-justified area of 10 characters with 2 decimal places and commas in the thousands place. item = "car" cost = 99,999.99 print('{:<10} {:>10}'.format(item, cost))
🌐
Python
docs.python.org › 3 › library › string.html
string — Common string operations — Python 3.14.7 documentation
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.
🌐
W3Schools
w3schools.com › python › ref_string_ljust.asp
Python String ljust() Method
Note: In the result, there are actually 14 whitespaces to the right of the word banana. The ljust() method will left align the string, using a specified character (space is default) as the fill character.