Python Forum
Unsupported Format Character
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unsupported Format Character
#1
I got this error of unsupported format character, As shown:

Output:
ValueError Traceback (most recent call last) Input In [6], in <cell line: 6>() 4 pq = np.sum(p * np.log(p/q)) 5 qp = np.sum(q * np.log(q/p)) ----> 6 print('KL(P || Q) : %. pq)%.3f' % pq) 7 print('KL(Q || P) : %. pq)%.3f' % qp) ValueError: unsupported format character ' ' (0x20) at index 15
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
Reply
#2
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
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] unexpected character after line continuation character paul18fr 4 3,419 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,187 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,757 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Replace changing string including uppercase character with lowercase character silfer 11 6,217 Mar-25-2019, 12:54 PM
Last Post: silfer
  python 3 memoryview : unsupported format <d evason 0 2,992 Nov-21-2018, 04:27 PM
Last Post: evason
  SyntaxError: unexpected character after line continuation character Saka 2 18,578 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