Python swapping string characters and about immutables

preview_player
Показать описание
'str' does not support item assignment error

Python swapping string characters and immutables

How immutables operate
How to get indices and characters of a string and swap them
Рекомендации по теме
Комментарии
Автор

Regarding immutables:
Though i show you can still append, this appending actually creates a whole new variable and a POINTER to this new variable.
Therefor the original immutable is still unchanged.

therationalplanner
Автор

Another faster way i found out recently is to create a new variable, and make it be equal to the string (string1) as a List type. Since lists are mutable you can then do the first line I showed that didnt work then re-add that corrected list back in as a string.
X = list(string1)
X[indx_of_b], X[indx_of_a] = X[indx_of_a], X[indx_of_b]
Swapped_final_string = "".join(X)

therationalplanner