Python String is immutable sequence.
The immutable means that you cannot change a string in place:
S = 'test' >>> S = 'x' # Raises an error! TypeError: 'str' object does not support item assignment
To change a string, build and assign a new string.
S = 'test' S = S + 'TEST!' # To change a string, make a new one print( S ) S = S[:4] + 'Burger' + S[-1] print( S )# from www .j av a 2 s . co m
The following code assigns back the replaced string.
S = 'please' S = S.replace('pl', 't') print( S )
String methods generate new string objects.