def quoteStr(astr, escChar='\\', quoteChar='"'):
    """Escape all instances of quoteChar and escChar in astr
    with a preceding escChar and surrounds the result with quoteChar.

    Examples:
    astr = 'foo" \bar'
    quoteStr(astr) = '"foo\" \\bar"'
    quoteStr(astr, escChar = '"') = '"foo"" \bar"'

    This prepares a string for output.
    """
    if escChar != quoteChar:
        # escape escChar
        astr = astr.replace(escChar, escChar + escChar)
    # escape quoteChar and surround the result in quoteChar
    return quoteChar + astr.replace(quoteChar, escChar + quoteChar) + quoteChar