2

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.

5
  • 1
    "str.format is not an answer to this question" - rather than just assert it, could you say why not? Commented Nov 11, 2015 at 10:55
  • @jonrsharpe: I believe I did? That answer would be valid for a different question. The question is not how you can do something similar to this, the question is how this is possible with string interpolation. Commented Nov 11, 2015 at 11:35
  • You've surely been around long enough to know how often SO gets XY problems? People ask "how do I parse HTML with regex" on a daily basis, and the right answer isn't the one to their direct question! All it takes is "str.format is not an answer... because [reasons]" - is it not supported on the version you're using, for example? Commented Nov 11, 2015 at 11:50
  • That's why I was leading with "For a legacy system", I guess that can use some extra clarification. I agree with you that in many cases the question is indeed wrong, I was just trying to indicate that this is indeed the question I wanted to ask. Commented Nov 11, 2015 at 12:38
  • "Replacing string interpolation with str.format would require many users of the current library to modify the function calls" - does it? That seems odd. Can't you keep the current interface to your function and replace the internal % logic with str.format? If you need to edit them for the extra options anyway, why not switch to (more modern, more readable - IMO, at least) str.format at that point? Commented Nov 11, 2015 at 13:47

1 Answer 1

2

No, you can't do this. Per the documentation (emphasis mine):

  1. Minimum field width (optional). If specified as an '*' (asterisk), the actual width is read from the next element of the tuple in values, and the object to convert comes after the minimum field width and optional precision.

You need to be passing a tuple, not a dictionary, as values to use the * field width. This is then made explicit:

When the right argument is a dictionary (or other mapping type), ... no * specifiers may occur in a format (since they require a sequential parameter list).

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed, it actually just occurred to me to simply look at the source and there is nothing to indicate that this would work any other way. Thanks for the help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.