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
🌐
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....
🌐
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 - product = "Café Especial" quantity = 3 unit_price = 15.90 # Clean alignment for professional display print("|" + product.ljust(20) + "|") print("|" + str(quantity).center(20) + "|") print("|" + f"R${unit_price:.2f}".rjust(20) + "|") ... 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.
🌐
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.
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" >>> ...
🌐
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.
🌐
C# Corner
c-sharpcorner.com › article › ways-to-align-text-strings-in-python
Ways To Align Text Strings In Python
September 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: my_text = “Shift me.” print(format(my_text,’>25'))
Find elsewhere
🌐
SQLPad
sqlpad.io › tutorial › python-formatted-output
Python formatted output | SQLPad
April 29, 2024 - Python provides several ways to align strings. Here are the most common ones: Left alignment uses < and ensures that the text is aligned to the left of the specified width. Right alignment uses > and aligns the text to the right.
🌐
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 - print("Original Number: ", x) # Format and print the value of 'x' with left alignment, a width of 10 characters, and padding using spaces. print("Left aligned (width 10) :"+"{:< 10d}".format(x)) # Format and print the value of 'x' with right ...
🌐
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....
🌐
TutorialsPoint
tutorialspoint.com › how-to-align-text-strings-using-python
How to Align Text Strings using Python?
Python's text alignment feature helps create well-formatted output. When data varies in length, proper alignment makes output more readable and professional-looking. You can specify left, right, or center alignment with a reserved width for consistent formatting. F-strings use alignment symbols followed by width numbers ? ... # Left align with 30 character width text = "Left Aligned String!" print(f"{text:<30}") print(f"{'Hello':<20}World")
🌐
GeeksforGeeks
geeksforgeeks.org › python-string-ljust-rjust-center
Python String - ljust(), rjust(), center() - GeeksforGeeks
January 2, 2025 - ljust() method is used to align a string to the left side and fill the remaining space on the right with a specific character (or space by default). ... The return type of ljust() is a new string that is left-justified with padding.
🌐
LabEx
labex.io › tutorials › python-how-to-manipulate-python-string-alignment-419448
How to manipulate Python string alignment | LabEx
At LabEx, we recommend practicing string alignment techniques to enhance your Python text processing skills. text = "Hello" left_aligned = text.ljust(10) print(f"'{left_aligned}'") ## Output: 'Hello ' text = "World" right_aligned = text.rjust(10) print(f"'{right_aligned}'") ## Output: ' World' text = "Python" centered = text.center(10) print(f"'{centered}'") ## Output: ' Python ' text = "LabEx" custom_left = text.ljust(10, '-') custom_right = text.rjust(10, '*') print(f"'{custom_left}', '{custom_right}'") ## Left alignment print("{:<10}".format("Left")) ## Right alignment print("{:>10}".format
🌐
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 ...
🌐
Educative
educative.io › answers › how-to-format-strings-in-python
How to format strings in Python
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.
🌐
Scientifically Sound
scientificallysound.org › 2016 › 10 › 17 › python-print3
Take control of your Python print() statements: part 3 | Scientifically Sound
November 17, 2021 - The last thing we need to learn ... we can tell Python how we want to align our text or numbers in these spaces. We can left-adjust (<), ......
🌐
Tutorialspoint
tutorialspoint.com › python › string_ljust.htm
Python String ljust() Method
text = 'Programming' # left-aligning the string x = text.ljust(89) print('The string after aligning is:', x)
🌐
GoLinuxCloud
golinuxcloud.com › home › programming › python string format
Python String Format: f-strings, format(), %, Padding, and Precision
June 23, 2026 - text = "Python" num = 42 print(f"|{text:<10}|") print(f"|{text:>10}|") print(f"|{text:^10}|") print(f"|{num:*>8}|") print(f"|{num:0=8d}|") Output Close · < left align · > right align · ^ center align · A fill character can appear before ...