user19626400
user19626400

Reputation: 33

can only concatenate str (not "complex") to str

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

Answers (2)

Talon
Talon

Reputation: 371

To convert a complex number to string you have to do that

print(str(5+8j))

Upvotes: 0

jthulhu
jthulhu

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

Related Questions