在新行上打印带有变量值的自记录表达式

Joh*_*ohn 5 python python-3.x f-string

Python 3.8 允许使用自记录表达式和调试=,例如:

print(f'{myvar=}')
Run Code Online (Sandbox Code Playgroud)

是否可以在新行上打印输出?这对于具有多行输出的变量(例如数据框)非常有用,例如

import pandas as pd

df = pd.DataFrame({'animal': ['alligator', 'bee', 'falcon', 'lion', 'monkey']})

print(f'{df=}')
Run Code Online (Sandbox Code Playgroud)
df =
      animal
0  alligator
1        bee
2     falcon
3       lion
4     monkey
Run Code Online (Sandbox Code Playgroud)

Pat*_*ugh 5

如果将 f 字符串加三引号,则可以在 后面包含换行符=

print(f'''{df=
}''')
Run Code Online (Sandbox Code Playgroud)