79584886

Date: 2025-04-21 15:35:13
Score: 0.5
Natty:
Report link

This is not exactly what you asked for, but this is a popular question and more people may need what I needed.

To get the variable's name only, without its value (Python 3.8 and above):

def varname(var_name_from_fstring: str) -> str:
    return var_name_from_fstring.split("=", maxsplit=1)[0]

my_variable = "blah"
name = varname(f"{my_variable=}")
print(name)

# Prints "my_variable"
Reasons:
  • Blacklisted phrase (0.5): I need
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Noam-N