Change string char in-place python 3 methods?

admin
By -
0

 String is immutable so  its   cannot be modified but there some way out.

with python builtin method join and list helping


1.  List and Join (Conversion) data type

Code:

l = list(string)

    string = ''.join(l)
    return string

This method is more optimal than the slicing method

>>> string = string[:5] + "k" + string[6:]
>>> print string
abrackdabra

here this not optimal for large amount of modification frequent cause here we re-creating
string again and method 1 we assigning variable again with conversion and modification

we can use in-place algorithms with patterns or linear search but maybe that work

Post a Comment

0Comments

Put Your Thought or Query Here

Post a Comment (0)