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 OverflowUse 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:
#
#
#
#
#
#
Currently your code interpreted as below:
for i in range(6, 0, -1):
print ( ("{0:>"+str(i)) + ("}".format("#")))
So the format string is constructed of a single "}" and that's not correct. You need the following:
for i in range(6, 0, -1):
print(("{0:>"+str(i)+"}").format("#"))
Works as you want:
================ RESTART: C:/Users/Desktop/TES.py ================
#
#
#
#
#
#
>>>
Python allows nested formatting operators. When operating positionally, each positional argument is counted by where its open brace appears. So to use x for justifying "Hey! A" as desired, you can do:
"{:{}}Hey! You are {} blocks away.".format("Hey! A", x, x-6)
^^ These brackets fill in the desired width using the second positional arg
If you want to avoid thinking about the numbering in this case, you can name the argument providing the width, passing it via keyword, e.g. width:
"{:{width}}Hey! You are {} blocks away.".format("Hey! A", x-6, width=x)
You can see more examples under "Nesting arguments and more complex examples" here.
This isn't pretty, but it's one way:
x = 20
("{:"+str(x)+"}Hey! You are {} blocks away.").format("Hey! A", x-6)
# 'Hey! A Hey! You are 14 blocks away.'
Alternative syntax:
''.join(("{:", str(x), "}Hey! You are {} blocks away.")).format("Hey! A", x-6)
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)
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.
If you are using it in a formatted string with the format() method which is preferred over the older style ''% formatting
>>> 'One hundred and twenty three with three leading zeros {0:06}.'.format(123)
'One hundred and twenty three with three leading zeros 000123.'
See
http://docs.python.org/library/stdtypes.html#str.format
http://docs.python.org/library/string.html#formatstrings
Here is an example with variable width
>>> '{num:0{width}}'.format(num=123, width=6)
'000123'
You can even specify the fill char as a variable
>>> '{num:{fill}{width}}'.format(num=123, fill='0', width=6)
'000123'
There is a string method called zfill:
>>> '12344'.zfill(10)
0000012344
It will pad the left side of the string with zeros to make the string length N (10 in this case).
Basically always returning it with a length of 8
That's what format strings do:
>>> print(f"{'C30':>08s}")
00000C30
As a sidenote, to output any number as 8-digit hex:
>>> print(f"{100:>08X}")
00000064
>>> print(f"{1024:>08X}")
00000400
See the documentation:
for f-strings (the
f'I am an f-string'syntax);for formatting syntax (the
>08sand>08Xthing).
Use string function rjust():
print(test.rjust(8,'0'))