For a legacy system that's in use in quite a few places with the string interpolation system I need to implement some code that formats a string with a specific length. I am aware that a rjust or ljust can solve this, but I'm trying to answer the question whether this is possible with the standard string interpolation system.
Examples:
>>> '%0*d' % (5, 3)
'00003'
>>> '%(value)05d' % dict(value=3)
'00003'
Now the question is, how can I combine these two?
My failed attempts:
>>> '%(value)*d' % dict(value=(5, 3))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: * wants int
>>> '%(value)*d' % dict(value=3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
>>> '%(value)*d' % {'*': 5, 'value': 3}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
>>> '%(value)*d' % {'*value': 5, 'value': 3}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
The question is: how can the asterisk and keyword arguments be combined using string interpolation?
Note: str.format is not an answer to this question, I'm not looking for alternatives but simply to answer the question whether this is possible or not. Replacing string interpolation with str.format would require many users of the current library to modify the function calls which is a unfavourable option for the near future.
str.formatis not an answer to this question" - rather than just assert it, could you say why not?str.formatis not an answer... because [reasons]" - is it not supported on the version you're using, for example?%logic withstr.format? If you need to edit them for the extra options anyway, why not switch to (more modern, more readable - IMO, at least)str.formatat that point?