Using the f-string format becomes very easy nowadays.

If you were using

print(f'{token:10}')

And you want the 10 to be another variable (for example the max length of all the tokens), you would write

print(f'{token:{maxTokenLength}}')

In other words, enclose the variable within {}


In your particular case, all you need is this.

head = 'eggs', 'bacon', 'spam'  
w1, w2, w3 = 8, 7, 10  # column widths  

print(f'  {head[0]:>{w1}}  {head[1]:>{w2}}  {head[2]:>{w3}}')
print(f'  {"="*w1:>{w1}}  {"="*w2:>{w2}}  {"="*w3:>{w3}}')

Which produces

      eggs    bacon        spam
  ========  =======  ==========
Answer from Rub on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › format-a-number-width-in-python
Format a Number Width in Python - GeeksforGeeks
July 23, 2025 - This code demonstrates how to use f-strings in Python to format integers to fixed widths, with options to pad them with leading zeros or spaces, depending on the desired output format.
Discussions

Understanding width and precision in string Formatting
Pretty good article! Considering the weird intricacy nature of your article, I think it could have also mentioned format() as a function, which is actually a feature and also calls the format() method of an object, without all the fanciness if you’re just formatting one single string. More on reddit.com
🌐 r/pythontips
3
18
February 3, 2021
python - How to print a string at a fixed width? - Stack Overflow
Here's the Python Format Specification Mini-Language describing these format strings and additional options. For quick reference, the space in {0: <5} is the [fill] , the < is [align], and 5 is [width] 2014-11-23T06:08:22.893Z+00:00 More on stackoverflow.com
🌐 stackoverflow.com
Python Field Width
If you convert your number to a str before printing, you can use ljust to pad with spaces. https://docs.python.org/2/library/stdtypes.html#str.ljust But if you're trying to align things, your best bet would probably be to use a module for printing tables, or use something like the pandas module. More on reddit.com
🌐 r/learnprogramming
1
2
February 14, 2023
How to set a variable to = width or height in Python Processing? - Stack Overflow
I'm new to Python and have the following issue. Normally, if you want to define a variable with the height, width or width/2 of the window in Jave Processing, you can declare it as follows: int... More on stackoverflow.com
🌐 stackoverflow.com
🌐
LabEx
labex.io › tutorials › python-how-to-handle-string-width-in-python-419445
How to handle string width in Python | LabEx
In Python, string width refers to the visual space a string occupies when displayed, which is particularly important when dealing with text rendering, formatting, and internationalization.
🌐
STEMpedia
ai.thestempedia.com › home › python functions › width()
width() - Object Detection Library - Python Function
July 24, 2022 - width() function is from Object Detection library of PictoBlox Python. This function returns the width of the object detected. You can specify the object for which the value is needed. The position is mapped with the stage coordinates.
🌐
Oreate AI
oreateai.com › blog › using-width-in-python › b9385c47e981abe7ae214a6d6b2ce54a
Using Width in Python - Oreate AI Blog
December 22, 2025 - In Python, the term 'width' can be used in the following scenarios: 1. Formatting String Output: You can use 'width' to specify the minimum width of a string. When the output string's length is less than the specified width, Python will pad ...
🌐
Note.nkmk.me
note.nkmk.me › home › python
Get Image Size (Width, Height) with Python, OpenCV, Pillow (PIL) | note.nkmk.me
April 29, 2025 - h, w, _ = im.shape print('width: ', w) print('height:', h) # width: 400 # height: 225 ... Alternatively, you can directly access them by index.
🌐
Reddit
reddit.com › r/pythontips › understanding width and precision in string formatting
r/pythontips on Reddit: Understanding width and precision in string Formatting
February 3, 2021 -

Hello friends,

Today I got stuck on a formatting issue and decided to learn more about the applications of width and precision with string Formatting of floats, integers and strings.

I have put down my learnings in an article and would like to share with you. Do share if there are other intricacies that I have missed!

Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › turtle-width-function-in-python
turtle.width() function in Python | GeeksforGeeks
July 20, 2020 - The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. This method is used to set or return the line thickness. Set the line thickness to width or return it.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-get-linux-console-window-width-in-python
How to get Linux console window width in Python?
In the case of the subprocess module, we make use of the check_output method that will help in retrieving the width and height of the Linux window. ... import subprocess rowSize, columnSize = subprocess.check_output(['stty', 'size']).split() print(rowSize) print(columnSize) immukul@192 linux-questions-code % python code.py 38 130
🌐
OpenStax
openstax.org › books › introduction-python-programming › pages › 8-4-string-formatting
8.4 String formatting - Introduction to Python Programming | OpenStax
March 13, 2024 - The index field refers to the index of the argument. The width field refers to the minimum length of the string.
🌐
TutorialsPoint
tutorialspoint.com › how-to-format-a-floating-number-to-fixed-width-in-python
How to format a floating number to fixed width in Python?
November 13, 2024 - str.format(): This method allows formatting types inside the placeholders to control how the values are displayed. We can format a floating-point number to a fixed width by using Python's f-strings.
🌐
GitHub
github.com › jquast › wcwidth
GitHub - jquast/wcwidth: Python and C11 library that measures the width of strings in a terminal · GitHub
>>> # '♀️' emoji w/vs-16 >>> wcwidth.wcswidth('\u2640\ufe0f') 2 · Use function wcswidth() to determine the length of many, a string of unicode characters. See specification of character measurements.
Author: jquast
🌐
Programiz
programiz.com › python-programming › methods › string › ljust
Python String ljust()
The string ljust() method returns a left-justified string of a given minimum width.
🌐
Python
wiki.python.org › moin › Py3kStringFormatting
String formatting methods in Python 3000, based on PEP ...
November 15, 2008 - format_spec ::= [[fill]align][sign][0][width][.precision][type] fill ::= <a character other than '}'> align ::= "<" | ">" | "=" | "^" sign ::= "+" | "-" | " " width ::= integer precision ::= integer type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "x" | "X" | "%"
🌐
DEV Community
dev.to › erictleung › print-fixed-fields-using-f-strings-in-python-26ng
Print fixed fields using f-strings in Python - DEV Community
August 20, 2020 - To do so, you can use the syntax used in other Python formatting. init = 34 end = 253 print(f"You had this much money : ${init:5}") print(f"Now you have this much money : ${end:5}") # You had this much money : $ 34 # Now you have this much money : $ 253 # Spacing width 12345
🌐
Reddit
reddit.com › r/learnprogramming › python field width
r/learnprogramming on Reddit: Python Field Width
February 14, 2023 -

Trying to adjust the field width so that a two-digit number for move would be next to the comma, while a one-digit number would have a space in between the number and the comma. I want one space to the left for both one and two digit numbers.

Input:

print("iteration", f'{move:<3}', ",")

Sample output:

iteration 9 , (3 spaces between)

iteration 10 , (2 spaces between)

🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-find-width-and-height-of-an-image-using-python
How to find width and height of an image using Python? - GeeksforGeeks
July 23, 2025 - We read the image using cv2.imread(path) which loads it as a multi-dimensional array of pixel data. The .shape property returns the image dimensions with the first value as height and the second as width. Pillow (PIL) is a popular Python library for basic image processing.