You can do this using the str.format() method.

Copy>>> width = 20
>>> print("{:>{width}} : {:>{width}}".format("Python", "Very Good", width=width))
              Python :            Very Good

Starting from Python 3.6 you can use f-string to do this:

CopyIn [579]: lang = 'Python'

In [580]: adj = 'Very Good'

In [581]: width = 20

In [582]: f'{lang:>{width}}: {adj:>{width}}'
Out[582]: '              Python:            Very Good'
Answer from Sede on Stack Overflow
๐ŸŒ
DZone
dzone.com โ€บ data engineering โ€บ data โ€บ python string format examples
Python String Format Examples
January 23, 2020 - That brings us to Python's native string format() method. Introduced in Python 3, this method provides a simple way to construct and format strings with dynamic substitutions.
๐ŸŒ
Like Geeks
likegeeks.com โ€บ home โ€บ python โ€บ python string interpolation (make dynamic strings)
Python string interpolation (Make Dynamic Strings)
November 22, 2023 - This is a powerful feature in Python that enables you to create a dynamic string in Python by embedding or substituting the values of variables into the string at runtime. Python supports multiple ways to format strings and perform string ...
๐ŸŒ
Real Python
realpython.com โ€บ python-string-formatting
Python String Formatting: Available Tools and Their Features โ€“ Real Python
December 2, 2024 - String formatting is essential in Python for creating dynamic and well-structured text by inserting values into strings. This tutorial covers various methods, including f-strings, the .format() method, and the modulo operator (%). Each method ...
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ string.html
Common string operations โ€” Python 3.14.5 documentation
These nested replacement fields may contain a field name, conversion flag and format specification, but deeper nesting is not allowed. The replacement fields within the format_spec are substituted before the format_spec string is interpreted. This allows the formatting of a value to be dynamically ...
๐ŸŒ
Real Python
realpython.com โ€บ python-formatted-output
A Guide to Modern Python String Formatting Tools โ€“ Real Python
February 1, 2025 - In modern Python, you have f-strings and the .format() method to approach the tasks of interpolating and formatting strings. These tools help you embed variables and expressions directly into strings, control text alignment, and use custom format ...
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-24481.html
dynamic f-string example
you might want to put this script in your python examples folder. you won't likely ever need to do this, but if some day you need to dynamically change the format of an f-string at run time (normally, python constructs code from an f-string at compi...
๐ŸŒ
PyFormat
pyformat.info
PyFormat: Using % and .format() for great good!
With this site we try to show you the most common use-cases covered by the old and new style string formatting API with practical examples. All examples on this page work out of the box with with Python 2.7, 3.2, 3.3, 3.4, and 3.5 without requiring any additional libraries.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ string-formatting-in-python
String Formatting in Python - GeeksforGeeks
There are five different ways to perform string formatting in Python: Formatting with % Operator.
Published ย  March 18, 2026
๐ŸŒ
Python
peps.python.org โ€บ pep-3101
PEP 3101 โ€“ Advanced String Formatting | peps.python.org
This PEP proposes a new system for built-in string formatting operations, intended as a replacement for the existing โ€˜%โ€™ string formatting operator.
๐ŸŒ
sqlpey
sqlpey.com โ€บ python โ€บ top-4-ways-to-dynamically-format-strings-in-python
Top 4 Ways to Dynamically Format Strings in Python - sqlpey
December 6, 2024 - A: You can use the str.format() method, f-strings, or padding techniques with the % operator to dynamically adjust the width of formatted strings.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ string-formatting-in-python-using
Python Modulo String Formatting - GeeksforGeeks
March 12, 2024 - format() method in Python is a tool used to create formatted strings. By embedding variables or values into placeholders within a template string, we can construct dynamic, well-organized output.
๐ŸŒ
Developmentality
developmentality.wordpress.com โ€บ 2012 โ€บ 05 โ€บ 17 โ€บ hello-planet_name-creating-dynamicstrings-in-python
Hello {planet_name}: Creating strings with dynamic content in Python | Developmentality
May 17, 2012 - would produce โ€œhello worldโ€. Python has this feature built in to strings with the % operator. You can read more in depth about the [String Formatting Operations][] and its syntax, but at the very least you should memorize the flags %d (for integer types), %f (for floating point), and %s (for strings).
๐ŸŒ
Real Python
realpython.com โ€บ python-f-strings
Python's F-String for String Interpolation and Formatting โ€“ Real Python
November 30, 2024 - Pythonista! You can support multiple languages using string templates. Then, you can handle localized string formatting based on the userโ€™s locale. The .format() method will allow you to dynamically interpolate the appropriate strings depending on the userโ€™s language selection.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ string-formatting-in-python
Python String Formatting - How to format String? - GeeksforGeeks
format() method in Python is a tool used to create formatted strings. By embedding variables or values into placeholders within a template string, we can construct dynamic, well-organized output.
Published ย  August 21, 2024
Top answer
1 of 4
2

Using no packages and no modules:

nums= [[  3, 4, -4,  -8, -10, -12,], [  5, 5,  3,  -3,  -4, -44,], [ 34,-4,-34, -22,  22,  22]]

t = ['|' + ''.join('%4i' % i for i in row) + ' |' for row in nums]
hdr = '+' +  (len(t[0])-2) * '-' + '+'
print '\n'.join( [hdr] + t + [hdr] )

This produces the output:

+-------------------------+
|   3   4  -4  -8 -10 -12 |
|   5   5   3  -3  -4 -44 |
|  34  -4 -34 -22  22  22 |
+-------------------------+

How it works:

  • t = ['|' + ''.join('%4i' % i for i in row) + ' |' for row in nums]

    t contains everything except the top and bottom rows. At its heart, the numbers are formatted as fixed width and aligned according to specification %4i. %4i means allow four spaces, format as an integer, and align right. Many other specifications are possible. If you wanted, for example, to 5-space wide integers aligned left, use %-5i.

  • hdr = '+' + (len(t[0])-2) * '-' + '+'

    Now that the interior rows are saved in t, we can assemble the header and trailer lines. These lines begin and end with a plus sign. The rest are filled with -.

  • print '\n'.join( [hdr] + t + [hdr] )

    This adds the hdr string to the beginning and end of the list of rows t and then joins all the rows together with newline characters to make the final table.

More complex example

Let's format the above table but add the min, max, mean, and standard deviation for each row at the end of each row.

def mmmsd(row):
    mean=sum(row)/len(row)
    stddev = ( sum( (x-mean)**2.0 for x in row ) / float(len(row)) )**0.5
    return '%6i%6i%6.2f%6.2f' % (min(row), max(row), mean, stddev)

nums= [[  3, 4, -4,  -8, -10, -12,], [  5, 5,  3,  -3,  -4, -44,], [ 34,-4,-34, -22,  22,  22]]

stats = [mmmsd(row) for row in nums]
t = [10*' ' + '|' + ''.join('%6i' % i for i in row) + ' |' + st for row, st in zip(nums, stats)]
hdr = 10*' ' + '+' +  (len(t[0])-12 - len(stats[0])) * '-' + '+' + len(stats[0]) * ' '
print '\n'.join( [hdr] + t + [hdr] )

This produces the result:

          +-------------------------------------+
          |     3     4    -4    -8   -10   -12 |   -12     4 -5.00  6.18
          |     5     5     3    -3    -4   -44 |   -44     5 -7.00 17.23
          |    34    -4   -34   -22    22    22 |   -34    34  3.00 24.92
          +-------------------------------------+                        
2 of 4
2

This is what I like about Python - there is always something stopping you from reinventing the wheel.

For your use case, prettytable is a good fit:

import prettytable

l = [
    [3, 4, -4, -8, -10, -12],
    [5, 5, 3, -3, -4, -44],
    [34, -4, -34, -22, 22, 22]
]

table = prettytable.PrettyTable(header=False, vrules=prettytable.FRAME)
for row in l:
    table.add_row(row)

print table

Prints:

+----+----+-----+-----+-----+-----+
| 3  | 4  |  -4 |  -8 | -10 | -12 |
| 5  | 5  |  3  |  -3 |  -4 | -44 |
| 34 | -4 | -34 | -22 |  22 |  22 |
+----+----+-----+-----+-----+-----+

Also check Manually changing table style paragraph of the package documentation page.


There is also texttable, but it is less powerful in terms of tweaking the table look&feel:

import texttable

l = [
    [3, 4, -4, -8, -10, -12],
    [5, 5, 3, -3, -4, -44],
    [34, -4, -34, -22, 22, 22]
]

table = texttable.Texttable()
table.add_rows(l, header=False)

print table.draw()

Prints:

+----+----+-----+-----+-----+-----+
| 3  | 4  | -4  | -8  | -10 | -12 |
+----+----+-----+-----+-----+-----+
| 5  | 5  | 3   | -3  | -4  | -44 |
+----+----+-----+-----+-----+-----+
| 34 | -4 | -34 | -22 | 22  | 22  |
+----+----+-----+-----+-----+-----+

Another option is tabulate which introduces a set of pre-defined table formats, e.g. "grid":

from tabulate import tabulate

l = [
    [3, 4, -4, -8, -10, -12],
    [5, 5, 3, -3, -4, -44],
    [34, -4, -34, -22, 22, 22]
]

table = tabulate(l, tablefmt="grid")
print table

Prints:

+----+----+-----+-----+-----+-----+
|  3 |  4 |  -4 |  -8 | -10 | -12 |
+----+----+-----+-----+-----+-----+
|  5 |  5 |   3 |  -3 |  -4 | -44 |
+----+----+-----+-----+-----+-----+
| 34 | -4 | -34 | -22 |  22 |  22 |
+----+----+-----+-----+-----+-----+

Also see relevant threads:

  • Formatting text in tabular form with Python
  • A Text Table Writer/Printer for Python
๐ŸŒ
Vocal Media
vocal.media โ€บ education โ€บ mastering-python-f-strings-unleashing-the-power-and-tricks-of-dynamic-string-formatting
Mastering Python F-Strings: Unleashing the Power and Tricks of Dynamic String Formatting | Education
For example, Python's f-strings offer powerful formatting specifiers that allow precise control over the appearance of values, such as specifying width, precision, and alignment. JavaScript's string interpolation, on the other hand, primarily focuses on variable substitution and dynamic content insertion.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ string โ€บ format
Python String format()
There's an easier way to format dictionaries in Python using str.format(**mapping). # define Person dictionary person = {'age': 23, 'name': 'Adam'} # format age print("{name}'s age is: {age}".format(**person)) ** is a format parameter (minimum field width). You can also pass format codes like precision, alignment, fill character as positional or keyword arguments dynamically. # dynamic string format template string = "{:{fill}{align}{width}}" # passing format codes as arguments print(string.format('cat', fill='*', align='^', width=5)) # dynamic float format template num = "{:{align}{width}.{precision}f}" # passing format codes as arguments print(num.format(123.236, align='<', width=8, precision=2)) Output ยท
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ python โ€บ string formatting in python
String Formatting in Python - Scaler Topics
June 11, 2024 - For this, we just have to pass ... was fixed. To dynamically set the size of the output string, we can pass the size as input to the format() method....