def replace_right(source, target, replacement, replacements=None):
    """
    String replace rightmost instance of a string.

    Args:
        source (string): the source to perform the replacement on
        target (string): the string to search for
        replacement (string): the replacement string
        replacements (int or None): if an integer, only replaces N occurrences
                                    otherwise only one occurrence is replaced
    """
    return replacement.join(source.rsplit(target, replacements))