tags:

views:

51

answers:

3

hey guys, i want to perform the following operation:

b = 'random'

c = 'stuff'

a  = '%s' + '%s' %(b, c)

but i get the following error:

TypeError: not all arguments converted during string formatting

does any one of you know to do so ?

+3  A: 
'%s%s' % (b, c)

or

b + c

or the newstyle format way

'{0}{1}'.format(a, b)
sdolan
A: 

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'
>>> 
pyfunc
wow, thanks guys, i didn't know about the precedence part , i ended up doing it like : a = '%b%c' %(b,c)
paulo
@paulo: If all you're doing is concatenating strings, *please* just use `+`. Simple and clear.
Nick T
@Nick T: @paulo: The reason, I simply showed the various examples was that the intent of the code in question was not clear. The advices in the various replies and comments holds. If you are concatenating, do it directly, for formatting use "%" approach and even better the 'format' as suggested by sdolan.
pyfunc
A: 

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)

raceCh-