def printable_str(text, keep_newlines=False):
    '''Escape any control or non-ASCII characters from string.

    This function is intended for use with strings from an untrusted
    source such as writing to a console or writing to logs. It is
    designed to prevent things like ANSI escape sequences from
    showing.

    Use :func:`repr` or :func:`ascii` instead for things such as
    Exception messages.
    '''
    if isinstance(text, str):
        new_text = ascii(text)[1:-1]
    else:
        new_text = ascii(text)

    if keep_newlines:
        new_text = new_text.replace('\\r', '\r').replace('\\n', '\n')

    return new_text
Escape any control or non-ASCII characters from string.

    This function is intended for use with strings from an untrusted
    source such as writing to a console or writing to logs. It is
    designed to prevent things like ANSI escape sequences from
    showing.

    Use :func:`repr` or :func:`ascii` instead for things such as
    Exception messages.