1

I get the error

TypeError: cannot concatenate 'str' and 'Set' objects 

and the error is caused by my code

name=inst1name+'-'+setName 

I know the problem is: inst1name is a set object, however this error never come up before when I run the script.

Do you know why is this? and how can I solve it?

2
  • str(inst1name)? what do you expect to happen when trying to concat a set and a str? Commented Aug 21, 2014 at 11:33
  • "I know the problem is: inst1name is a set object." I think it's the other way around. if inst1name was a set and setName was a string, you'd get unsupported operand type(s) for +: 'set' and 'str'. You get cannot concatenate 'str' and 'set' objects when the first argument is a string and the second argument is a set. Commented Aug 21, 2014 at 11:44

1 Answer 1

1

You could explicitly convert the set to its string representation like this:

name = inst1name + '-' + str(setName)

But a better way would be to use string composition like this:

name = '%s-%s' % (inst1name, setName)

Or even string.format like this:

name = '{}-{}'.format(inst1name, setName)
Sign up to request clarification or add additional context in comments.

1 Comment

thank you! I also tried your method to solve a similar problem, and this is the code after change: str(setsTuple) #setsTuple is a set muscleNodes=setsTuple[1] however the same error comes out, why is it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.