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

Viewed 57

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)
2 Answers

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.

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

print(str(5+8j))
Related