hhh
hhh

Reputation: 52850

Spacified columnized on-the-left line formatting to columnized data?

xxxxxxxxxxxxx1.11xxxxxxxxxx1.11xxxxxxxxxxx1.11
xxxxxxxxxxxxx1.11xxxxxxxxxx1.11xxxxxxxxxx11.11
  1. Is there some ready module in Python with which I could easily do above like formatting 17signs, 14 signs and then 15 signs?
  2. How can you get the thing n.b. the small difference with 1.11 and 11.11?

N.b. the numbers could be larger but not greater than 10 signs. The linewise count 46 (=17+14+15) is constant.

[Update] Notice that I am using Python 2.6.5 so getting error ValueError: zero length field name in format with suggestions.

Upvotes: 1

Views: 461

Answers (3)

kelloti
kelloti

Reputation: 8951

You don't need a module or library to do this, use string formatting. To avoid the ValueError, specify what index your parameter is in the supplied values:

"{0:>17}".format(11.1)

Upvotes: 2

hhh
hhh

Reputation: 52850

Someone commented the right answer but removed it so I will post it here. I will remove it if s/he post it so the credits go to the right person.

('%17.2f%14.2f%15.2f' % (1.11, 1.11, 11.11))

Upvotes: 1

miku
miku

Reputation: 188114

String formatting is built-in:

Examples for Python 2.7/3.1

>>> '{:x>17}'.format(s)
'xxxxxxxxxxxx11.11'

>>> '{:x>17}{:x>14}{:x>15}'.format(11.1, 11.1, 11.1)
'xxxxxxxxxxxxx11.1xxxxxxxxxx11.1xxxxxxxxxxx11.1'

Upvotes: 2

Related Questions