import re


def commify(num, separator=','):
    """
    Return a string representing the number num with separator inserted for
    every power of 1000.

    commify(1234567) -> '1,234,567'

    Args:
        num: Number to be formatted.
        separator: Separator to be used.

    Returns:
        "Commified" string.
    """
    regex = re.compile(r'^(-?\d+)(\d{3})')
    num = str(num)  # just in case we were passed a numeric value
    more_to_do = 1
    while more_to_do:
        substring = rf'\1{separator}\2'
        (num, more_to_do) = regex.subn(substring, num)
    return num