theta
theta

Reputation: 25641

Apply formatting after string formatting

I don't know if it's possible with common string formatting or advanced string formatting, so thought to ask...

I have this simplified snippet:

>>> s = 'some string'
>>> y = 10
>>> '<td>%s</td><td>%4d</td>' % (s, y)
'<td>some string</td><td>  10</td>'

I want to pad numeric cell with &nbsp; as my html backend doesn't accept text aligning. Is there easy way to format numerical value with &nbsp; instead empty space?

Upvotes: 1

Views: 276

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 994947

You could use something like:

"%s%d" % ("&nbsp;" * (4 - len(str(y))), y)

Upvotes: 2

Related Questions