Python - String to list conversion

Introduction

You can convert the string to an object that does support in-place changes:

Demo

S = 'testmy' 
L = list(S) 
print( L )

Result

The built-in list function builds a new list out of the items in any sequence.

Here, it explodes the characters of a string into a list.

You edit the list and then convert back to string.

Demo

S = 'testmy' 
L = list(S) # from  w ww . j a  va  2 s.c  o  m

L[3] = 'x'                        # Works for lists, not strings 
L[4] = 'x' 
print( L )

S = ''.join(L) 
print( S )

Result

The join method is from string not list.

It uses the empty string delimiter to convert from a list back to a string

Related Topic