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.
๐ŸŒ
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" >>> ...
๐ŸŒ
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'))
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-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.
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.
๐ŸŒ
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-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
๐ŸŒ
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)