python 3.8.0 – print self-documenting expression with value of variable on a new line
Question:
Python 3.8.0 allows for self-documenting expressions and debugging using =
, e.g.: print(f'{myvar=}')
.
Is it possible to print the output on a new line? this would be useful for variables with multi-line outputs like dataframes.
e.g.
>>> df = pd.DataFrame({'animal':['alligator', 'bee', 'falcon', 'lion',
'monkey', 'parrot', 'shark', 'whale', 'zebra']})
>>> print(f'{df.head()=}')
df.head() =
animal
0 alligator
1 bee
2 falcon
3 lion
4 monkey
Answers:
If you make your f string triple-quoted, you can include a newline after the =
:
df = pd.DataFrame({'animal':['alligator', 'bee', 'falcon', 'lion',
'monkey', 'parrot', 'shark', 'whale', 'zebra']})
print(f'''{df=
}''')
Using triple quotes works, but it causes some trouble when the print statement is in an indented block:
if True:
if True:
# ...some code with indentation...
print(f"""{df =
}""")
# ...more code with indentation...
Note that one does not simply indent the }"""
part to the proper level, without breaking the formatting of the first line of the f-string.
I asked a similar question, and found a single line solution here, which I am writing here as well, for reference.
df = pd.DataFrame({'animal':['alligator', 'bee', 'falcon', 'lion',
'monkey', 'parrot', 'shark', 'whale', 'zebra']})
f_str_nl = lambda object: f"{chr(10) + str(object)}" # add n directly
# f_str_nl = lambda object: f"{os.linesep + str(object)}" # add rn on windows
print(f"{f_str_nl(df) = !s}")
which outputs:
f_str_nl(df) =
animal
0 alligator
1 bee
2 falcon
3 lion
4 monkey
5 parrot
6 shark
7 whale
8 zebra
This method does not cause problems with indented blocks.
However, the name of the object in the f-string gets wrapped with the function name, f_str_nl()
.
Python 3.8.0 allows for self-documenting expressions and debugging using =
, e.g.: print(f'{myvar=}')
.
Is it possible to print the output on a new line? this would be useful for variables with multi-line outputs like dataframes.
e.g.
>>> df = pd.DataFrame({'animal':['alligator', 'bee', 'falcon', 'lion',
'monkey', 'parrot', 'shark', 'whale', 'zebra']})
>>> print(f'{df.head()=}')
df.head() =
animal
0 alligator
1 bee
2 falcon
3 lion
4 monkey
If you make your f string triple-quoted, you can include a newline after the =
:
df = pd.DataFrame({'animal':['alligator', 'bee', 'falcon', 'lion',
'monkey', 'parrot', 'shark', 'whale', 'zebra']})
print(f'''{df=
}''')
Using triple quotes works, but it causes some trouble when the print statement is in an indented block:
if True:
if True:
# ...some code with indentation...
print(f"""{df =
}""")
# ...more code with indentation...
Note that one does not simply indent the }"""
part to the proper level, without breaking the formatting of the first line of the f-string.
I asked a similar question, and found a single line solution here, which I am writing here as well, for reference.
df = pd.DataFrame({'animal':['alligator', 'bee', 'falcon', 'lion',
'monkey', 'parrot', 'shark', 'whale', 'zebra']})
f_str_nl = lambda object: f"{chr(10) + str(object)}" # add n directly
# f_str_nl = lambda object: f"{os.linesep + str(object)}" # add rn on windows
print(f"{f_str_nl(df) = !s}")
which outputs:
f_str_nl(df) =
animal
0 alligator
1 bee
2 falcon
3 lion
4 monkey
5 parrot
6 shark
7 whale
8 zebra
This method does not cause problems with indented blocks.
However, the name of the object in the f-string gets wrapped with the function name, f_str_nl()
.