Print statement tricks

30 May 2022

print variable name, type and output in one

f"{variable_name=}

replace a printed line

30 Jul 2022

construct a string starting with \r and adding end='' to the print statement:

import time
for x in range(10):
    print('\r H M S 0 0 ' + str(x), end='')
    time.sleep(1)

output:

print-replace-line.gif

Example with my standard count print statements:

for x in ...:
    count += 1
    print("\r" + str(count), end='')

print current line number with Python

from inspect import currentframe

def get_linenumber():
    cf = currentframe()
    return cf.f_back.f_lineno

print(f"This is line number {get_linenumber()})

print progress bar for loops

from tqdm import tqdm

for x in tqdm(data):

links

social