Use f-strings

for i in range(6, 0, -1): 
    print(f"{'#':>{i}}")

or:

Use format() (instead of concatenating strings)

for i in range(6, 0, -1): 
    print("{0:>{1}}".format("#", i))

Both solutions give the output:

     #
    #
   #
  #
 #
#
Answer from Camion on Stack Overflow
🌐
Nmt
infohost.nmt.edu › tcc › help › pubs › python › web › format-var-length.html
9.4.5. Formatting a field of variable length
April 24, 2013 - >>> n = 42 >>> d = 8 >>> "{0:{1}d}".format(42, 8) ' 42' >>> "{0:0{1}d}".format(42, 8) '00000042' >>> You can, of course, also use keyword arguments to specify the field width. This trick also works for variable precision.
🌐
Python
docs.python.org › 3 › library › string.html
Common string operations — Python 3.14.7 documentation
The built-in string class provides the ability to do complex variable substitutions and value formatting via the format() method described in PEP 3101.
Top answer
1 of 4
48

This is a carryover from the C formatting markup:

print "%*s, blah" % (max_title_width,column)

If you want left-justified text (for entries shorter than max_title_width), put a '-' before the '*'.

>>> text = "abcdef"
>>> print "<%*s>" % (len(text)+2,text)
<  abcdef>
>>> print "<%-*s>" % (len(text)+2,text)
<abcdef  >
>>>

If the len field is shorter than the text string, the string just overflows:

>>> print "<%*s>" % (len(text)-2,text)
<abcdef>

If you want to clip at a maximum length, use the '.' precision field of the format placeholder:

>>> print "<%.*s>" % (len(text)-2,text)
<abcd>

Put them all together this way:

%
- if left justified
* or integer - min width (if '*', insert variable length in data tuple)
.* or .integer - max width (if '*', insert variable length in data tuple)
2 of 4
22

You have the new strings formatting methods from Python 3 and Python 2.6.

Starting in Python 2.6, the built-in str and unicode classes provide the ability to do complex variable substitutions and value formatting via the str.format() method described in PEP 3101. The Formatter class in the string module allows you to create and customize your own string formatting behaviors using the same implementation as the built-in format() method.

(...)

For example, suppose you wanted to have a replacement field whose field width is determined by another variable:

>>> "A man with two {0:{1}}.".format("noses", 10)
"A man with two noses     ."
>>> print("A man with two {0:{1}}.".format("noses", 10))
A man with two noses     .

So for your example it would be

max_title_width = max(len(text) for text in columns)

for column in columns:
    print "A man with two {0:{1}}".format(column, max_title_width)

I personally love the new formatting methods, as they are far more powerful and readable in my humble opinion.

Find elsewhere
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-string-formatters-in-python-3
How To Use String Formatters in Python 3 | DigitalOcean
This tutorial will guide you through some of the common uses of string formatters in Python, which can help make your code and program more readable and user…
🌐
Stack Overflow
stackoverflow.com › questions › 65229379 › passing-variable-length-parameters-to-formatted-string-with-variable-number-of-p
python - Passing variable-length parameters to formatted string with variable number of placeholders - Stack Overflow
strng_frmt = strng_multiple.format(tel,per,'{}') # strng_frmt == 'Telephone 1234 Contact Person Jhon Address {}' This only works if you know there is a third value that you don't have though ... No. It is not true. 2020-12-10T06:20:19.3Z+00:00 ... You dont need to give a third variable. If {} is a string that needs to be printed, python format allows you to add those by doubling up those curly brackets.
🌐
GeeksforGeeks
geeksforgeeks.org › python › pad-or-fill-a-string-by-a-variable-in-python-using-f-string
Pad or fill a string by a variable in Python using f-string - GeeksforGeeks
July 15, 2025 - This means the string will move to the far right, and the padding character will be added before it until the string reaches the desired total length. ... s = 'GFG' char = '*' # Padding character # Pad string to the left using a variable left_padded = f"{s:{char}>10}" print(left_padded)
🌐
Python
peps.python.org › pep-3101
PEP 3101 – Advanced String Formatting | peps.python.org
Shell variable syntax: $name and ... many others. When used without the braces, the length of the variable is determined by lexically scanning until an invalid character is found....
🌐
PyFormat
pyformat.info
PyFormat: Using % and .format() for great good!
In Python 3 there exists an additional conversion flag that uses the output of repr(...) but uses ascii(...) instead. class Data(object): def __repr__(self): return 'räpr' ... By default values are formatted to take up only as many characters as needed to represent the content. It is however also possible to define that a value should be padded to a specific length...
🌐
Scaler
scaler.com › home › topics › python › string formatting in python
String Formatting in Python - Scaler Topics
June 11, 2024 - To simply display a signed or unsigned ... using Python format() method. For displaying a number with left alignment, we use the “:<n” symbol inside the placeholder in the format() method. Here n is the total length of the required output string....
🌐
Python
docs.python.org › 3.4 › library › string.html
6.1. string — Common string operations — Python 3.4.10 documentation
The built-in string class provides the ability to do complex variable substitutions and value formatting via the format() method described in PEP 3101.