SyntaxError with starred expression when unpacking a tuple on its own for string formatting

The error occurs because (a) is just a value surrounded by parenthesis. It’s not a new tuple object.

Thus, '%d %d' % (*a) is equivalent to '%d %d' % * a, which is obviously wrong in terms of python syntax.

To create a new tuple, with one expression as an initializer, use a comma after that expression:

>>> '%d %d' % (*a,)
'1 2'

Of course, since a is already a tuple, we can use it directly:

>>> '%d %d' % a
'1 2'

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)