![]() |
Unsupported Format Character - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Unsupported Format Character (/thread-38232.html) |
Unsupported Format Character - Led_Zeppelin - Sep-19-2022 I got this error of unsupported format character, As shown: I do not see it. Could it be a space before the colon? It calls it on the first print line. I am guessing it would also call it on the second print line if it ever got there. Help appreciated. Respectfully, LZ RE: Unsupported Format Character - Led_Zeppelin - Sep-19-2022 No, the unsupported format character is not it. I just tried removing the space before the colon It still calls an error. Unsupported format character. R, LZ RE: Unsupported Format Character - deanhystad - Sep-20-2022 The error is exactly where the error message says. Python is looking for a format character to associate with the first "%". It will grab whatever comes next. So the problem is that you have an unsupported format character (space)I'm not sure what you are trying to do here. It is a bit of a mess. print('KL(P || Q) : %. pq)%.3f' % pq)Since there is only one variable to print there can only be one % in the format string. The first % is incorrect. Did you want this? print("KL(P || Q) : %% pq)%.3f" % pq) or this?print("KL(P || Q) : %.3f pq)" % pq) or maybeprint("KL(P || Q) : %.3f" % pq) Modulo string formatting is ancient and has been replaced (twice!). f'string formatting is much easier to understand.print(f"KL(P || Q) : {pq:.3}"With f'string formatting the variables appear inside the string, in the same spot they will appear in the final string. Modulo (%) and format() style formatting had the variables appear at the end where it was easy to mess up the order. I also like the clear demarkation the curly brackets provide, separating formatting from the rest of the string. |