Skip to content

How to Center Text Dynamically in Python

Learn how to center text in Python using .center() methods, F-strings (f"{val:^{width}}"), and dynamic width calculations for CLI formatting and report generation.

String alignment is a fundamental skill when building Command Line Interfaces (CLIs), generating reports, or logging formatted data. While simple concatenation can achieve this, it is error-prone and tedious. Python provides built-in methods and formatting literals to handle text alignment dynamically.

This guide explores the standard .center() method, modern F-string formatting, and how to calculate widths dynamically based on content.

Method 1: Using the .center() Method

The string object in Python has a built-in method called .center(width, fillchar). It returns a new string centered in a string of length width. Padding is done using the specified fillchar (default is a space).

Basic Usage

text = "Python"

width = 20
padding = (width - len(text)) // 2
centered_manual = " " * padding + text + " " * padding
print(f"|{centered_manual}|")

centered_auto = text.center(20)
print(f"|{centered_auto}|")

title = "Menu"
print(title.center(20, "="))

Output:

|       Python       |
|       Python       |
========Menu========

Introduced in Python 3.6, F-strings provide a concise syntax for alignment. The ^ symbol denotes center alignment.

Syntax: f"{value:^width}"

Dynamic Padding with Nested Braces

To use a variable for the width inside an F-string, you must nest the braces: f"{value:^{variable}}"

text = "TutRef"
width = 15

print(f"|{text:^15}|")

print(f"|{text:^{width}}|")

print(f"{text:-^{width}}")

Output:

|    TutRef     |
|    TutRef     |
----TutRef-----

Calculating Widths Dynamically

In real-world scenarios, you rarely know the width beforehand. Usually, you want to center text based on the longest item in a list or the terminal size.

Centering Based on Longest Item

words = ["Apple", "Banana", "Watermelon", "Kiwi"]

max_width = max(len(w) for w in words) + 4

print(f"Max Width: {max_width}")
print("-" * max_width)

for w in words:
    print(f"|{w.center(max_width - 2)}|")
    
print("-" * max_width)

Output:

Max Width: 14
--------------
|   Apple    |
|   Banana   |
| Watermelon |
|    Kiwi    |
--------------

Practical Example: Creating a CLI Table

Let's combine these concepts to create a formatted table where headers and data are centered dynamically.

data = [
    ("Alice", "Engineer"),
    ("Bob", "Designer"),
    ("Charlie", "Manager")
]

headers = ("Name", "Role")

col1_width = max(len(headers[0]), max(len(row[0]) for row in data)) + 4
col2_width = max(len(headers[1]), max(len(row[1]) for row in data)) + 4

print(f"|{headers[0]:^{col1_width}}|{headers[1]:^{col2_width}}|")
print("-" * (col1_width + col2_width + 3))

for name, role in data:
    print(f"|{name:^{col1_width}}|{role:^{col2_width}}|")

Output:

|   Name    |    Role    |
--------------------------
|   Alice   |  Engineer  |
|    Bob    |  Designer  |
|  Charlie  |  Manager   |

Conclusion

Dynamically centering strings improves the user experience of console applications.

  1. Use .center(width) for simple, readable string manipulation.
  2. Use F-strings (f"{val:^{width}}") for concise formatting, especially when combining variables.
  3. Calculate max() length first to ensure consistent alignment across a list of items.
Navigation