Python Tip #288 (of 365): Show a value's repr with "!r" in f-strings. >>> name =...

@treyhunner@mastodon.social

Python Tip #288 (of 365):

Show a value's repr with "!r" in f-strings.

>>> name = "Trey"
>>> print(f"Name: {name}")
Name: Trey
>>> print(f"Name: {name!r}")
Name: 'Trey'

By default, f-strings use the human-readable representation of an object (str instead of repr). The "!r" suffix forces the repr version.

Most objects have the same str and repr, but some (like strings and datetime‍.date objects) don't.

🧵 (1/4)

#Python #DailyPythonTip