开发者

combining strings using string substitution - python

开发者 https://www.devze.com 2023-01-19 15:55 出处:网络
hey guys, i want to perform the following operation: b = \'random\' c = \'stuff\' a= \'%s\' + \'%s\' %(b, c)

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号