Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can string value in Python 3.8 short-handed f-string be without quotes?

>>> long_variable_name = 'foo'
>>> f'long_variable_name={long_variable_name}'
'long_variable_name=foo'
>>> f'{long_variable_name=}'
"long_variable_name='foo'"

Is it possible to have the second short-handed expression f'{long_variable_name=}' produce the same output as the first expression? I.e. without the quotes for the string value.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Looks like the {var=} syntax defaults to calling an object’s
__repr__, so you can explicitly declare it call an object’s __str__, eg:

long_variable_name = 'foo'
f'{long_variable_name=:s}'

Gives you:

'long_variable_name=foo'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading