0

It is common to use format strings (or f-strings) to print variables for debugging. However one needs to repeat the argument names if they should be printed as well.

[first_name, last_name, age] = ["Randall", "Munroe", 37]
print(f"first_name={first_name} last_name={last_name} age={age}")

Is there a shorter way to produce the same (or similar) output of names and values with f-strings?

I believe I have seen a short form however I did not find any mention in the format string syntax.

Pyfisch
  • 1,752
  • 1
  • 17
  • 29
  • Does this answer your question? [f-string debugging shorthand in Python 3.6](https://stackoverflow.com/questions/65296925/f-string-debugging-shorthand-in-python-3-6) – mkrieger1 Jun 05 '22 at 20:48
  • 1
    Or better: https://stackoverflow.com/questions/59661904/what-does-equal-do-in-f-strings-inside-the-expression-curly-brackets – mkrieger1 Jun 05 '22 at 20:49
  • @mkrieger1 The linked question answers my question well. – Pyfisch Jun 05 '22 at 21:06

1 Answers1

0

A shorter form (aka "shorthand") is to write {a=}instead of a={a}.

[first_name, last_name, age] = ["Randall", "Munroe", 37]
print(f"{first_name=} {last_name=} {age=}")
Pyfisch
  • 1,752
  • 1
  • 17
  • 29