def thousand_to_k(x): if x >= 1000: return thousand_comma_separated(x // 1000) + 'k' return str(x) def thousand_comma_separated(x): return "{:,}".format(x)