python string in-place modification
Question: folks,

Suppose I want to change 'abc' to 'bac' in Python. What would be the best way
to do it?

I am thinking of the following

    
    
    tmp = list('abc')
    tmp[0],tmp[1] = tmp[1],tmp[0]
    result = ''.join(tmp)
    


Answer: You are never editing a string "in place", strings are immutable.

You could do it with a list but that is wasting code and memory.

Why not just do:

    
    
    x = 'abc'
    result = x[1] + x[0] + x[2:]
    

or (personal fav)

    
    
    import re
    re.sub('(.)(.)', r'\2\1','abc')
    

This might be cheating, but if you really want to _edit in place_ , and are
using 2.6 or older, then use MutableString(this was deprecated in 3.0).

    
    
    from UserString import MutableString
    x = MutableString('abc')
    x[1], x[0] = x[0], x[1]
    >>>>'bac'
    

With that being said, solutions are generally not as simple as 'abc' = 'bac'
You might want to give us more details on how you need to split up your
string. Is it always just swapping first digits?

