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
🌐
Note.nkmk.me
note.nkmk.me › home › python
Right-justify, Center, Left-justify Strings and Numbers in Python | note.nkmk.me
May 18, 2023 - Format strings and numbers with format() in Python · To right-justify, center, or left-justify, use [CHARACTER][DIRECTION][STRING_LENGTH] as the format string. Use >, ^, or < to indicate the direction. s = 'abc' print('right : {:*>8}'.format(s)) ...
🌐
Vultr Docs
docs.vultr.com › python › standard-library › str › rjust
Python str rjust() - Right-Justify String | Vultr Docs
December 31, 2024 - This procedure aligns each column's headers and data entries to the right, ensuring all elements are vertically aligned and properly formatted. The str.rjust() method in Python simplifies the process of aligning text to the right, a common ...
Discussions

python - Format output string, right alignment - Stack Overflow
I am processing a text file containing coordinates x, y, z 1 128 1298039 123388 0 2 .... every line is delimited into 3 items using words = line.split() After processing ... More on stackoverflow.com
🌐 stackoverflow.com
How do I right-justify strings with dollar signs and percentages?
"Formatting doesn't seem to do literally anything at all." Well, all of Python's number formatting methods allow you to restrict the output to two decimal places and to right justify it. I suggest that you post your code so that people here can advise you what is going wrong with it. You can edit your original post to include it. Guide to posting code in this subreddit: https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F More on reddit.com
🌐 r/learnpython
8
3
February 13, 2022
Left-justifying floating-point number output
Question: Is the “-” in the format used below supposed to left-justify the number? Code: This section is just generating numbers to demonstrate the problem. def neuralNetwork(input, weight): prediction = input * weight return prediction ​weight = 0.5 goalPrediction = 0.8 input = 0.5 for ... More on discuss.python.org
🌐 discuss.python.org
4
0
November 5, 2021
Formatting text to be justified in Python 3.3 with .format() method - Stack Overflow
I'm new to Python and trying to work on some sample scripts. I'm doing a simple a cash-register type thing but I want to justify or right align the output so that it looks something like this: sub... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Stack Overflow
stackoverflow.com › questions › 14638430 › format-right-justification-in-python
Format Right Justification in Python - Stack Overflow
So how do I get those values to print right justified while the rest of the string before each is left justified? Thank you for your help you guys are awesome as always! ... This is the first thing in the documentation for str.format(). Whenever you don't know something about Python (or any language) the documentation should be your first port of call.
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
🌐
Delft Stack
delftstack.com › home › howto › python › right justify string in python
How to Right Justify String in Python | Delft Stack
February 20, 2025 - Instead of f-strings, we can also use the format() method to right justify strings in python.
🌐
Real Python
realpython.com › python-formatted-output
A Guide to Modern Python String Formatting Tools – Real Python
February 1, 2025 - You align text in Python string formatting using the align component, which can justify text to the left, right, or center within a specified width.
Find elsewhere
🌐
Python.org
discuss.python.org › python help
Left-justifying floating-point number output - Python Help - Discussions on Python.org
November 5, 2021 - Question: Is the “-” in the format used below supposed to left-justify the number? Code: This section is just generating numbers to demonstrate the problem. def neuralNetwork(input, weight): prediction = input * weight return prediction ​weight = 0.5 goalPrediction = 0.8 input = 0.5 for iteration in range(124): prediction = neuralNetwork(input, weight) error = (prediction - goalPrediction) ** 2 directionAndAmount = (prediction - goalPrediction) * input weight = weight - directionA...
🌐
GeeksforGeeks
geeksforgeeks.org › python › string-alignment-in-python-f-string
String Alignment in Python f-string - GeeksforGeeks
July 15, 2025 - 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 ...
🌐
Programiz
programiz.com › python-programming › methods › string › rjust
Python String rjust() (With Examples)
# right aligns 'Python' up to width ... equal to the length of the string, the original string is returned. ... In the above example, we have used the rjust() method to right justify the text string....
🌐
Vultr Docs
docs.vultr.com › python › standard-library › str › ljust
Python str ljust() - Left-Justify String | Vultr Docs
December 26, 2024 - The str.ljust() method in Python is a straightforward yet versatile function used for string manipulation, specifically for left-justifying text within a specified width.
🌐
Medium
shweta-lodha.medium.com › ways-to-align-text-strings-in-python-ef0f1dab5a28
Ways To Align Text Strings In Python | by Shweta Lodha | Medium
August 13, 2022 - Let’s have a look at those: For left-aligned text, you can use ljust(…) function, which stands for left justification. ... The above lines will display left-aligned text with a length of 25.
🌐
Tutorialspoint
tutorialspoint.com › python › string_rjust.htm
Python String rjust() Method
When we pass the total string length as width and letters as the fillchar parameter, the method returns the justified string to its right
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python-string-ljust-rjust-center
Python String - ljust(), rjust(), center() - GeeksforGeeks
January 2, 2025 - The return type of ljust() is a new string that is left-justified with padding. ... s = "Burger" # Padding the name with dashes until length is 20 formatted_item = s.ljust(20, "-") print(formatted_item)
🌐
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 - If you want to set it back to 4 or 5 decimal points , you will have to set the format back to which ever format you want. Till then it will continue to display only 2 decimal points. Numpy has settings to change the decimal point precision to a desired one. ... Lets see the output as an extension to the previous example from pandas . ... Sadrach Pierre, Ph.D. ... Take the crash course in the 'whys' and 'whens' of using Deep Learning in Time Series Analysis. ... You don't always need Python or R to fit models - Postgresql has covered the basics.