This is not trivial. This can be done using row indexing and slicing. But you will also have to take into account the capitalization in place and keep it.
>>> s = "Hello" >>> x = list(s) >>> x ['H', 'e', 'l', 'l', 'o']
Change w / third and swap case first
>>> x[0], x[2] = x[2].upper(), x[0].lower() >>> x ['L', 'e', 'l', 'h', 'o']
Change second to fourth
>>> x[1], x[3] = x[3], x[1] >>> x ['L', 'l', 'h', 'e', 'o']
Return it to the string
>>> ''.join(x) 'Llheo'
This will not work for all cases, but in this particular case this is what you requested.
Edit: Let's take it one step further with the function:
def swap_letters(indexes, string): """ @indexes should be a list of 2-tuples of indexes to swap, eg: [(0,2), (1,3)] @string is the input string. """ letters = list(string) for src, dst in indexes: letter_src = letters[src] letter_dst = letters[dst]
source share