string formatting - Python 3 format method - tuple index out of range -


i have problem format method in python 3.4.2. shows me following error:

traceback (most recent call last):   python shell, prompt 2, line 3 builtins.indexerror: tuple index out of range 

the code:

a = "{0}={1}" b = ("str", "string") c = a.format(b) print (c) 

the tuple contains 2 strings indexes 0 , 1 , error should not displayed.

according docs should passing in arguments positional arguments, not tuple. if want use values in tuple, use * operator.

str.format(*args, **kwargs) 

perform string formatting operation. string on method called can contain literal text or replacement fields delimited braces {}. each replacement field contains either numeric index of positional argument, or name of keyword argument. returns copy of string each replacement field replaced string value of corresponding argument.

"the sum of 1 + 2 {0}".format(1+2) 'the sum of 1 + 2 3'

more specifically, you'd want following:

a = "{0}={1}" b = ("str", "string") c = a.format(*b) print (c) 

or

a = "{0}={1}" c = a.format("str", "string") print (c) 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -