Apply same format function to each python print() parameter I have a python print statement and inside the string I wish to print are four digits. I would like to apply the same formatting function to each param. I am not familiar with the latest and greatest features of python PEPs. 

Is there a slick way to do this?

Code

statement = "Splitting up the {} file into {} chunks, with the filesystem block size of {}, causing {} extra space to be used"

print(statement.format(
    sizeof_fmt(input_file_size), 
    sizeof_fmt(chunk_size), 
    sizeof_fmt(block_size), 
    sizeof_fmt(surrendered_space)))


Format Function

def sizeof_fmt(num, suffix='B'):
    for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
        if abs(num) &lt; 1024.0:
            return "%3.1f%s%s" % (num, unit, suffix)
        num /= 1024.0
    return "%.1f%s%s" % (num, 'Yi', suffix)
Time will tell if your question is considered a worthy Code Review question, but till then I'ld like you to give a short review on your code nevertheless.

Format function

You could reduce the code duplication in the format function and make use of .format or f-strings (from Python 3.6 onwards).

def sizeof_fmt_rev(num, suffix='B'):
    for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
        if abs(num) &lt; 1024.0:
            break
        num /= 1024.0
    else:
        # this part is only executed if the loop was not left with a break
        unit = 'Yi'
    return f"{num:.1f}{unit}{suffix}"


This uses for ... else, one of the less well-known features of Python and only has a single line where the format expression has to be written. I see a chance to build something using math.log instead of that loop, but I will leave that as an exercise to you. You can even build something that works without a loop, but at least the version I came up with (found below) is actually slower than the original implementation.

def sizeof_fmt_rev_log(num, suffix='B'):
    exponent = min(int(math.log(abs(num), 1024)), 8)
    num /= 1024**exponent
    unit = ('', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi')[exponent]
    return f"{num:.1f}{unit}{suffix}"


I used

for i in range(10):
    num = 3.8 * 1024**i
    print(sizeof_fmt_rev(num))
    assert sizeof_fmt(num) == sizeof_fmt_rev(num)
    assert sizeof_fmt(-num) == sizeof_fmt_rev(-num)


to test the revised version.

Code

As @AJNeufeld mentions in his comment, you could use map to save yourself some typing

print(
    statement.format(*map(sizeof_fmt, (input_file_size, chunk_size, block_size, surrendered_space)))
)


which is functionally equivalent to using a list comprehension:

print(
    statement.format(*[
        sizeof_fmt(i)
        for i in (input_file_size, chunk_size, block_size, surrendered_space)
    ])
)


Both build upon a technique called tuple unpacking, but as you can see it can also be used with lists, other sequences, and maybe also iterables (if it is a generator, it will be consumed - thanks @Graipher, who confirmed it/pointed it out in a comment).