str.format() is making your fields left aligned within the available space. Use alignment specifiers to change the alignment:
'<'Forces the field to be left-aligned within the available space (this is the default for most objects).
'>'Forces the field to be right-aligned within the available space (this is the default for numbers).
'='Forces the padding to be placed after the sign (if any) but before the digits. This is used for printing fields in the form โ+000000120โ. This alignment option is only valid for numeric types.
'^'Forces the field to be centered within the available space.
Here's an example (with both left and right alignments):
>>> for args in (('apple', '$1.09', '80'), ('truffle', '$58.01', '2')):
... print '{0:<10} {1:>8} {2:>8}'.format(*args)
...
apple $1.09 80
truffle $58.01 2
Answer from Steven Rumbalski on Stack Overflowstr.format() is making your fields left aligned within the available space. Use alignment specifiers to change the alignment:
'<'Forces the field to be left-aligned within the available space (this is the default for most objects).
'>'Forces the field to be right-aligned within the available space (this is the default for numbers).
'='Forces the padding to be placed after the sign (if any) but before the digits. This is used for printing fields in the form โ+000000120โ. This alignment option is only valid for numeric types.
'^'Forces the field to be centered within the available space.
Here's an example (with both left and right alignments):
>>> for args in (('apple', '$1.09', '80'), ('truffle', '$58.01', '2')):
... print '{0:<10} {1:>8} {2:>8}'.format(*args)
...
apple $1.09 80
truffle $58.01 2
With python3 f-strings (not your example but mine):
alist = ["psi", "phi", "omg", "chi1", "chi2", "chi3", "chi4", "chi5", "tau"]
for ar in alist:
print(f"{ar: >8}", end=" ")
print()
for ar in alist:
ang = ric.get_angle(ar)
print(f"{ang:8.4}", end=" ")
print()
generates
psi phi omg chi1 chi2 chi3 chi4 chi5 tau
4.574 -85.28 178.1 -62.86 -65.01 -177.0 80.83 8.611 115.3
Since Python 2.6+, you can use a format string in the following way to set the columns to a minimum of 20 characters and align text to right.
table_data = [
['a', 'b', 'c'],
['aaaaaaaaaa', 'b', 'c'],
['a', 'bbbbbbbbbb', 'c']
]
for row in table_data:
print("{: >20} {: >20} {: >20}".format(*row))
Output:
a b c
aaaaaaaaaa b c
a bbbbbbbbbb c
data = [['a', 'b', 'c'], ['aaaaaaaaaa', 'b', 'c'], ['a', 'bbbbbbbbbb', 'c']]
col_width = max(len(word) for row in data for word in row) + 2 # padding
for row in data:
print "".join(word.ljust(col_width) for word in row)
a b c
aaaaaaaaaa b c
a bbbbbbbbbb c
What this does is calculate the longest data entry to determine the column width, then use .ljust() to add the necessary padding when printing out each column.
Is something like this acceptable?
>>> names = ["Firstname Lastname", "First Last", "Name Name"]
>>> scores = [49900, 93000, 6400]
>>> for i,v in enumerate(zip(names, scores)):
... name, score = v[0], v[1]
... print "% *d. % -*s %d" % (3, i, 30, name, score)
...
0. Firstname Lastname 49900
1. First Last 93000
2. Name Name 6400
Here the "name" field is padded with spaces to a max width of 30 characters.
Edit: I now see that the width of the font is also a problem. I did not realize that at first from your question. I'll leave this up in case future Googlers end up here for a different reason.
You have two options:
Change the font of the PyGTK label to a font that has equal width characters (not unreasonable in a game, it reminds us of the old arcade days). You can do this with
set_markupofpango.Layout.Use two labels next to each other and use the method
set_alignmentof the classpango.Layout. The first label aligns the name to the left, the second label contains the score and it aligns to the right. As long as their is enough space the names and scores will align nicely to the left and right respectively.
You should be able to use the format method:
"Location: {0:20} Revision {1}".format(Location, Revision)
You will have to figure out the format length for each line depending on the length of the label. The User line will need a wider format width than the Location or District lines.
Try %*s and %-*s and prefix each string with the column width:
>>> print "Location: %-*s Revision: %s" % (20,"10-10-10-10","1")
Location: 10-10-10-10 Revision: 1
>>> print "District: %-*s Date: %s" % (20,"Tower","May 16, 2012")
District: Tower Date: May 16, 2012
import pandas as pd
pd.options.display.float_format = '${:,.2f}'.format
df = pd.DataFrame([123.4567, 234.5678, 345.6789, 456.7890],
index=['foo','bar','baz','quux'],
columns=['cost'])
print(df)
yields
cost
foo $123.46
bar $234.57
baz $345.68
quux $456.79
but this only works if you want every float to be formatted with a dollar sign.
Otherwise, if you want dollar formatting for some floats only, then I think you'll have to pre-modify the dataframe (converting those floats to strings):
import pandas as pd
df = pd.DataFrame([123.4567, 234.5678, 345.6789, 456.7890],
index=['foo','bar','baz','quux'],
columns=['cost'])
df['foo'] = df['cost']
df['cost'] = df['cost'].map('${:,.2f}'.format)
print(df)
yields
cost foo
foo $123.46 123.4567
bar $234.57 234.5678
baz $345.68 345.6789
quux $456.79 456.7890
If you don't want to modify the dataframe, you could use a custom formatter for that column.
import pandas as pd
pd.options.display.float_format = '${:,.2f}'.format
df = pd.DataFrame([123.4567, 234.5678, 345.6789, 456.7890],
index=['foo','bar','baz','quux'],
columns=['cost'])
print df.to_string(formatters={'cost':'${:,.2f}'.format})
yields
cost
foo $123.46
bar $234.57
baz $345.68
quux $456.79