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: 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 ...
๐ŸŒ
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.
Discussions

Find the most likely word alignment between two strings in Python
Looks like a modification to Levenshtein distance should work. More on reddit.com
๐ŸŒ r/learnpython
4
5
July 30, 2022
How to align text centered on a character?
You can use the string formatting mini-language in an f-string or .format() string method. It's possible to left or right justify values in a given field width, along with many other neat manipulations. Here's a simple example showing how, try running it: # make up some dummy data: (label, value) data = (("longer name", 42), ("short", 3.14159), ("really really long", -5.1), ) label_width = 20 # must be at least width of longest label for (label, value) in data: print(f"{label:>{label_width}s}: {value}") Edit: grammar. More on reddit.com
๐ŸŒ r/learnpython
6
5
August 1, 2024
f-string alignment
It works for me, mate. Maybe your computer is broken. >>> print(f"{'today':25}" + "xx") today xx >>> print(f"{'tomorrow':25}" + "xx") tomorrow xx >>> print(f"{'the day after tomorrow':25}" + "xx") the day after tomorrow xx Actually, looking at it a bit it's pretty clear that your browser is not using a monospace font. More on reddit.com
๐ŸŒ r/learnpython
6
1
April 29, 2023
Python packages/functions to extract pairwise alignments from SAM/BAM?
Use pysam to extract the sequence of the read, and then from the FASTA file you can extract the reference sequence at the same positions. The CIGAR string will tell you how to align them. Or just look at it on IGV. More on reddit.com
๐ŸŒ r/bioinformatics
3
3
August 15, 2018
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
Let's create a simple Python script to demonstrate the basic alignment techniques. Open the WebIDE in your LabEx environment. Create a new file named basic_alignment.py in the /home/labex/project directory by clicking on the "New File" icon in the WebIDE. ... ## basic_alignment.py print("Basic String Alignment Examples") print("-" * 30) ## Left alignment example print("Left alignment:") print("Python".ljust(20) + "|") print("Programming".ljust(20) + "|") print("Alignment".ljust(20) + "|") print() ## Right alignment example print("Right alignment:") print("Python".rjust(20) + "|") print("Programming".rjust(20) + "|") print("Alignment".rjust(20) + "|") print() ## Center alignment example print("Center alignment:") print("Python".center(20) + "|") print("Programming".center(20) + "|") print("Alignment".center(20) + "|")
๐ŸŒ
Shikshasanchar
shikshasanchar.com โ€บ courses โ€บ python โ€บ strings โ€บ alignment-methods
String Alignment methods in Python - ShikshaSanchar
Returns a new string that is center-aligned in a string of specified width. Extra space is filled with optional characters like *, -, + etc (default is space). ... Width = 11 means the total string length becomes 11. Original string "Python" has 6 characters, so 5 extra spaces are added (or ...
๐ŸŒ
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") ... # Right align with 30 character width text = "Right Aligned String!" print(f"{text:>30}") print(f"{'Hello':>20}World")
๐ŸŒ
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 - These multiline strings can be ... text aligns correctly within your code. Multiline strings in Python are text blocks that span multiple lines. You can create them using triple-quotes (โ€˜โ€™โ€™ or โ€œโ€โ€) or by using Fstring with line breaks. ... message = f"""This is a multiline string. It can span multiple lines and is enclosed in triple-quotes. This is particularly useful for creating long, structured text.""" print(message) In this example, we create ...
Find elsewhere
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ python โ€บ string formatting in python
String Formatting in Python - Scaler Topics
June 11, 2024 - Here n is the total length of the required output string. For example, if you have several lengths 4 (5 including the โ€“ sign) and have to pad it with spaces to make its length 10 while printing, you can do it as follows. ... The period โ€œ.โ€ in the output occurs after 5 space characters.
๐ŸŒ
Bentley
cissandbox.bentley.edu โ€บ sandbox โ€บ wp-content โ€บ uploads โ€บ 2022-02-10-Documentation-on-f-strings-Updated.pdf pdf
A Guide to Formatting with f-strings in Python - CIS Sandbox
also be specified. These are shown in the following example: ... Program output is often required to be in tabular form. f-strings are very useful in formatting ยท this kind of output. The following lines produce a tidily aligned set of columns with integers and
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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....
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ string.html
string โ€” Common string operations
This is equivalent to a fill character of '0' with an alignment type of '='.
๐ŸŒ
Smiansh
smiansh.com โ€บ blogs โ€บ master-string-formatting-in-python-p3-f-strings
Master string formatting in Python using F-Strings
October 23, 2024 - String alignment is also an important part of string formatting. We can align the string in three positions: Left, Center and Right aligned. See the example below to understand it in depth.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ string โ€บ rjust
Python String rjust() (With Examples)
# right aligns 'Python' up to width 10 using '*' result = text.rjust(10, '*') print(result) # Output: ***Python ... Here, rjust() right aligns string using fillchar up to width. ... Note: If width is less than or equal to the length of the string, the original string is returned.
๐ŸŒ
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....
๐ŸŒ
Dummies
dummies.com โ€บ article โ€บ technology โ€บ programming-web-design โ€บ python โ€บ how-to-format-strings-in-python-148335
How to Format Strings in Python | dummies
December 29, 2021 - You can use these alignments: ... #: Specifies that the output should use the alternative display format for numbers. For example, hexadecimal numbers will have a 0x prefix added to them.
Author:
๐ŸŒ
Finxter
blog.finxter.com โ€บ 5-best-ways-to-align-text-strings-using-python
5 Best Ways to Align Text Strings Using Python โ€“ Be on the Right Side of Change
February 27, 2024 - In this code snippet, the string โ€œAlignMeโ€ is aligned to the left, right, and center within a width of 10 characters, using โ€˜-โ€˜ as the padding character. These methods update the string to be printed in the format specified. Introduced in Python 3.6, f-strings offer a new way to format strings that is both concise and readable.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-format-strings-in-python
How to format strings in Python
By default, strings are left-aligned within the field, and numbers are right-aligned.