def charWrap(s, width, hanging=0):
    """Word wrap a string.

    Return a new version of the string word wrapped with the given width
    and hanging indent. The font is assumed to be monospaced.

    This can be useful for including text between <pre> </pre> tags,
    since <pre> will not word wrap, and for lengthy lines, will increase
    the width of a web page.

    It can also be used to help delineate the entries in log-style
    output by passing hanging=4.
    """
    if not s:
        return s
    if hanging < 0 or width < 1 or hanging >= width:
        raise ValueError("Invalid width or indentation")
    hanging = ' ' * hanging
    lines = s.splitlines()
    i = 0
    while i < len(lines):
        s = lines[i]
        while len(s) > width:
            lines[i], s = s[:width].rstrip(), hanging + s[width:].lstrip()
            i += 1
            lines.insert(i, s)
        i += 1
    return '\n'.join(lines)