Reputation: 33
Why I am getting this error (TypeError: can only concatenate str (not "complex") to str? I am not doing any kind of concatenation in my code. I am confused, please help me.
print("%d" % 5+8j)
Upvotes: 1
Views: 253
Reputation: 371
To convert a complex number to string you have to do that
print(str(5+8j))
Upvotes: 0
Reputation: 8678
You need parenthesis
print("%s" % (5+8j))
Otherwise, it's understood as
print(("%s" % 5)+8j)
Incidentally, there is no point in using %d
instead of %s
, as it's just less general.
Upvotes: 2