I'm looking for a way to determine how many positional arguments a string has when used with format()
.
For example:
>>> "{0}, {1} {0}".format("Bond", "James")
Bond, James Bond
>>> number_of_positionals("{0}, {1} {0}")
2
>>> number_of_positionals("{0}, {key} {1} {2}")
3
I can implement number_of_positionals
myself, but not being fully familiar with all the ins and outs of the format()
mini-language, I'm worried about missing out on edge-cases.
Is there a built-in or otherwise solid implementation of this available in Python 2.6 or 2.7?