How to Print With Column Alignment in Python

Manav Narula Feb 02, 2024
  1. Use the % Formatting to Print With Column Alignment in Python
  2. Use the format() Function to Print With Column Alignment in Python
  3. Use f-strings to Print With Column Alignment in Python
  4. Use the expandtabs() Function to Print With Column Alignment in Python
  5. Use the just() Function to Print With Column Alignment in Python
  6. Conclusion
How to Print With Column Alignment in Python

The print() function has evolved in Python. It started as a statement in Python 3, and it was transformed into a function.

We can format the result with different methods to print something in a specific format. This tutorial will demonstrate how to print with column alignment in Python.

Use the % Formatting to Print With Column Alignment in Python

The % method is one of the most common and oldest ways to format strings and get results in the required style. We can use the %-*s to specify the spacing that will work as the column width.

The spacing needs to be adjusted for every row.

Code Example:

print("First Name: %-*s Last Name: %s" % (13, "Jim", "Clark"))
print("Age: %-*s Website: %s" % (20, "42", "DelftStack.com"))

Output:

First Name: Jim            Last Name: Clark
Age: 42                    Website: DelftStack.com

Use the format() Function to Print With Column Alignment in Python

We can utilize the format() function to print results in our desired style and format. To print something with column alignment in Python, we must specify the same number of spaces for every column.

This can be achieved with the format() function. We can specify the unknown values within the function and use the curly braces to specify the spaces.

Code Example:

print("First Name: {0:13} Last Name: {1}".format("Jim", "Clark"))
print("Age: {0:20} Website: {1}".format("42", "DelftStack.com"))

Output:

First Name: Jim           Last Name: Clark
Age: 42                   Website: DelftStack.com

In the above example, we print the details for a user using the format() function and align the result. Note that we have to specify different spaces for the first and second rows.

This is to manage the difference in the length of the column name.

Use f-strings to Print With Column Alignment in Python

The f-strings were introduced in Python 3 as a way to format strings. These are relatively faster than the previous methods.

We can also use the f-strings to specify the spacing as column width with the print() function.

Code Example:

print(f"{'First Name: ' + 'Jim':<25} Last Name: {'Clark'}")
print(f"{'Age: ' + '42':<25} Website: {'DelftStack.com'}")

Output:

First Name: Jim           Last Name: Clark
Age: 42                   Website: DelftStack.com

In the above code example, the result is printed in perfect alignment. The advantage of using this method is that we do not have to specify different spacing for different rows.

Use the expandtabs() Function to Print With Column Alignment in Python

We can use escape characters to add new lines, tabs, and other characters while printing something. The \t character is used to specify a tab.

We can use this escape character with the expandtabs() function. This function can explicitly specify the tab’s spacing and column alignment in Python.

Code Example:

print(("First Name: Jim" + "\t" + "Last Name: Clark").expandtabs(13))
print(("Age: 42" + "\t" + "Website: DelftStack.com").expandtabs(26))

Output:

First Name: Jim           Last Name: Clark
Age: 42                   Website: DelftStack.com

Use the just() Function to Print With Column Alignment in Python

In Python, we have different methods for string alignment. We can align the strings using the ljust(), rjust, and center() functions.

Using these, we can align the rows appropriately based on our requirements. This example will demonstrate the ljust() function.

This function will left align the string. The spacing is specified within the function and can be the same for the two rows to align them as columns.

Code Example:

print("First Name: Jim".ljust(40) + "Last Name: Clark")
print("Age: 42".ljust(40) + "Website: DelftStack.com")

Output:

First Name: Jim                         Last Name: Clark
Age: 42                                 Website: DelftStack.com

In the above example, we align the strings left with the ljust() function, and they appear to be aligned as columns. Similarly, we can use the other functions for alignment.

Conclusion

To wrap up, we discussed several methods to print strings aligned as columns in Python. For this, we first discussed different string-formatting methods to achieve this.

Out of these, the fstrings proved to be the most easy-to-use method. We also discussed spacing using the expandtabs() function, and different functions like ljust() also work perfectly for alignment-related problems.

Author: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn

Related Article - Python Print