Python Forum
Unsupported Format Character
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unsupported Format Character
#3
The error is exactly where the error message says.
Error:
KL(P || Q) : %. pq)%.3f ^Here's the error, right at index 15 in the format string
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)
Output:
KL(P || Q) : % pq)10.123
or this?
print("KL(P || Q) : %.3f pq)" % pq)
Output:
KL(P || Q) : 10.123 pq)
or maybe
print("KL(P || Q) : %.3f" % pq)
Output:
KL(P || Q) : 10.123
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.
Reply


Messages In This Thread
Unsupported Format Character - by Led_Zeppelin - Sep-19-2022, 03:48 PM
RE: Unsupported Format Character - by Led_Zeppelin - Sep-19-2022, 03:55 PM
RE: Unsupported Format Character - by deanhystad - Sep-20-2022, 01:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] unexpected character after line continuation character paul18fr 4 3,617 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,283 Jul-13-2020, 07:05 PM
Last Post: snippsat
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,827 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Replace changing string including uppercase character with lowercase character silfer 11 6,394 Mar-25-2019, 12:54 PM
Last Post: silfer
  python 3 memoryview : unsupported format <d evason 0 3,041 Nov-21-2018, 04:27 PM
Last Post: evason
  SyntaxError: unexpected character after line continuation character Saka 2 18,707 Sep-26-2017, 09:34 AM
Last Post: Saka

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020