Python Forum
f string concatenation problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
f string concatenation problem
Hi Group, I'm new. So hopefully I'll figure out how to navigate this forum and get the help I need as I'm trying to learn python. Currently, I'm having a problem with what I believe is called f-string concatenation. I'm using pycharm to code python as I'm working my way through a book called Neural Networks from Scratch. The code I've typed out is long.... but the snippet I'm having a problem with is this:
print(f'epoch:{epoch}, ' +
                 f"acc: {accuracy: .3f}, ' +
                 f'loss: {loss: .3f}, ' +
                 f'lr: {optimizer.current_learning_rate}')
The error is:
Traceback (most recent call last): File "C:\Users\........", line 465, in <module> f'loss: {loss: .3f}, ' + TypeError: unsupported format string passed to NoneType.__format__
I think that pycharm perhaps doesn't like the way it's set up. I don't know. Can someone explain what's gone wrong or how I can fix it?

buran write Jun-28-2021, 04:54 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
It's telling you that instead of being a number as you suppose, one of your variables (probably loss) is None. Trying to apply a numeric format to None generates an error.
Actually, I think I understand now. It wasn't apparent at first.

Actually your code shows that you don't understand f-strings. There is no need of concatenation.
print(f'epoch:{epoch}, acc: {accuracy: .3f}, loss: {loss: .3f}, lr: {optimizer.current_learning_rate}')
This is assuming you fix the problem with loss being None.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert string to float problem vasik006 8 671 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  String concatenation in SQL update statement hammer 3 582 Feb-24-2022, 08:00 PM
Last Post: hammer
Question Problem with string and \n Falassion 6 1,447 Jun-15-2021, 03:59 PM
Last Post: Falassion
  how to deal with problem of converting string to int usthbstar 1 1,263 Jan-05-2021, 01:33 PM
Last Post: perfringo
  Concatenation ?? ridgerunnersjw 1 1,077 Sep-26-2020, 07:29 PM
Last Post: deanhystad
  string problem Mathisdlg 6 1,885 Aug-05-2020, 09:31 AM
Last Post: Mathisdlg
  Unicode string index problem luoheng 6 1,830 Nov-23-2019, 03:04 PM
Last Post: luoheng
  simple string & input problem kungshamji 5 2,328 Jun-23-2019, 03:54 PM
Last Post: kungshamji
  Combining two strings together (not concatenation) DreamingInsanity 6 2,000 Mar-29-2019, 04:32 PM
Last Post: DreamingInsanity
  Problem with inserting a string in to Sqlite db darktitan 3 3,190 Mar-03-2019, 06:30 PM
Last Post: stranac

Forum Jump:

User Panel Messages

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