Added the way to modify immutable string. (#25334)

* Added the way to modify immutable string.

* Formatting changes
This commit is contained in:
Mukesh Jha
2018-12-19 08:54:09 +05:30
committed by Manish Giri
parent 932fc1d56b
commit d7918dbc00

View File

@ -20,7 +20,15 @@ Python allows `str` objects, or _strings_, to be expressed in a few different wa
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
Instead, you can convert the string into a list, modify the list element (string character) you wish to change, and then join the list elements back to a string, like so:
>>> foo = "my string"
>>> foo_list_form = list(foo)
>>> foo_list_form[0] = "a"
>>> foo = ' '.join(foo_list_form)
>>> print(foo)
ay string # The required output
## Reference:
<a href='https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str' target='_blank' rel='nofollow'>Text Sequence Type _str_</a>