def swap(string, nameA, nameB):
    """ Swap all occurances of nameA with nameB
    and vice-versa in the string.

    Parameters
    ----------
    string : str
        The string to modify.
    nameA : str
        The substring to replace with `nameB`.
    nameB : str
        The substring to replace with `nameA`.

    Returns
    -------
    str
        The modified string.

    Examples
    --------
        >>> qosy.swap('A Up B Dn', 'Up', 'Dn') # 'A Dn B Up'
        >>> qosy.swap('X X A', 'X', 'Y') # 'Y Y A'
        >>> qosy.swap('1 2 3', '1', '3') # '3 2 1'
    """
    
    result = string.replace(nameA, '({})'.format(nameA))
    result = result.replace(nameB, nameA)
    result = result.replace('({})'.format(nameA), nameB)

    return result