Added the way to modify immutable string. (#25334)
* Added the way to modify immutable string. * Formatting changes
This commit is contained in:
@ -20,6 +20,14 @@ 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:
|
||||
|
||||
|
Reference in New Issue
Block a user