0

I wonder why f'{None}_some_string' not throw an error?

Here is some example to reproduce the problem:

s1 = None # <class 'NoneType'>
s2 = 'real_string' # <class 'str'>

s1 + s2 # TypeError: unsupported operand type(s) for +: 'NoneType' and 'str


f'{s1}_bla'

'None_bla'

f'{s2}_bla'

'real_string_bla'

Is it possible to make expressions like f'{s}_some_string' throw an error when s is None?

Barmar
  • 741,623
  • 53
  • 500
  • 612
mrgloom
  • 20,061
  • 36
  • 171
  • 301

1 Answers1

2

By default f-string apply the str() (string) function.

f'{None}_some_string'

is equivalent to...

f'{str(None)}_some_string}'
Florian Fasmeyer
  • 795
  • 5
  • 18