Converting bytes into other units

def convert(byte_count, unit): units = ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] idx = units.index(unit) if idx == -1: return 'Wrong unit provided' return "%.3f" % (byte_count / (1 << (idx+1)*10)) + unit print(convert(1024, 'kB')) # 1.000kB print(convert(7397230, 'MB')) # 7.055MB print(convert(12328345634, 'GB')) # 11.482GB print(convert(8836745903456334, 'PB')) # 7.849PB