hey guys, i want to perform the following operation:
b = 'random'
c = 'stuff'
a = '%s' + '%s' %(b, c)
but i get the following error开发者_StackOverflow:
TypeError: not all arguments converted during string formatting
does any one of you know to do so ?
'%s%s' % (b, c)
or
b + c
or the newstyle format
way
'{0}{1}'.format(a, b)
Depending on what you want :
>>> b = 'random'
>>> c = 'stuff'
>>> a = '%s' %b + '%s' % c
>>> a
'randomstuff'
>>>
>>> b + c
'randomstuff'
>>>
>>> z = '%s + %s' % (b, c)
>>> z
'random + stuff'
>>>
Due to operator precedence, your program is first trying to substitue b and c into second '%s'. Therefore splitting such strings with + is meaningless, it's better to do
a = '%s %s' % (b,c)
精彩评论