As you correctly state, the lines are immutable and cannot be changed in place, but we can create a new line with replaced characters. Here is one idea: let's convert a string to a list, swap elements in a list, and then convert the list back to string:
def swap(s, i, j): lst = list(s); lst[i], lst[j] = lst[j], lst[i] return ''.join(lst)
Another possible implementation is to manipulate the string using slices and indexes:
def swap(s, i, j): return ''.join((s[:i], s[j], s[i+1:j], s[i], s[j+1:]))
In any case, it works as expected:
swap('abcde', 1, 3) => 'adcbe'
source share