1

What is the most pythonic way to insert a variable number of arguments into a string?

For example:

Given a variable date format that may be of the form: YYYY-MM-DD,YYYY-MM-DD HH,YYYY-MM-DD HH:MM or YYYY-MM-DD HH:MM:SS.

I would like to insert the values for year, month, day, hour, minute, second into a string, similar to a statement like this:

"%d/%d/%d %d:%d:%d" % (variable number of arguments here)

Such that a print statement of the above string would output a date which, where possible, the actual values are inserted, and if they are not available, a default value of 0 is inserted.

Please note I am not looking for a solution to this particular example, I am looking for a solution to the more general question I state at the beginning (which should work for the example provided). I'm also looking for a solution that doesn't involve an if...elif...else block.

2
  • 1
    I think keyword arguments are what you're looking for. Commented Oct 24, 2014 at 1:27
  • I have expanded my answer further, how is that? Commented Oct 24, 2014 at 1:38

2 Answers 2

2

It's more Pythonic to use the str.format method rather than the old %s printf style from C. It's also more Pythonic to use the builtin modules:

>>> import datetime
>>> args1 = 2014, 10, 01
>>> args2 = 2014, 10, 01, 12
>>> args3 = 2014, 10, 01, 12, 25
>>> '{0}'.format(datetime.datetime(*args1))
'2014-10-01 00:00:00'
>>> '{0}'.format(datetime.datetime(*args2))
'2014-10-01 12:00:00'
>>> '{0}'.format(datetime.datetime(*args3))
'2014-10-01 12:25:00'

If you want a more generalized answer to

What is the most pythonic way to insert a variable number of arguments into a string?

Then you could define a function to do that, with positional arguments for the required arguments, and keyword arguments for the optional arguments:

def format_args(arg1, arg2, arg3, arg4=0, arg5=0, arg6=0):
    return '{0}, {1}, {2}, {3}, {4}, {5}'.format(
      arg1, arg2, arg3, arg4, arg5, arg6)

usage:

>>> format_args(*args1)
'2014, 10, 1, 0, 0, 0'
>>> format_args(*args2)
'2014, 10, 1, 12, 0, 0'
>>> format_args(*args3)
'2014, 10, 1, 12, 25, 0'
Sign up to request clarification or add additional context in comments.

Comments

1

Agree that using datetime is preferable to home rolled solutions. To answer the more general question of interpolating variable arguments in format strings that have different numbers of arguments: the % operator requires a specific number of arguments, but string.format only requires the minimum number. Provide a function that will pad your starting arguments to the longest possible with defaults, then you can feed that to format.

from itertools import chain

date_only = [ 2014, 2, 29 ]
date_and_time = [ 2014, 10, 23, 4, 20, 0 ]
defaults = [ 0 ] * 6 # the most arguments your format string requires

date_strs = [ "{} {} {}", "{} {} {} {} {} {}" ]

for s in date_strs:
    print(s.format(*chain(date_only, defaults)))
    print(s.format(*chain(date_and_time, defaults)))

Output:

2014 2 29
2014 10 23
2014 2 29 0 0 0
2014 10 23 4 20 0

Comments

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.