Strings
Strings are immutable in Python
,
which means that we cannot edit them.
In fact, the following code will throw an exception:
>>> s = "hello, world!"
>>> s[0] = "H"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
The solution is to use a list together with the join
function to build up the string.
Notice that append
takes and join
takes ,
where is the number of elements in the list
(considering that each element is a string of length 1
).
>>> lst = []
>>> lst.append("h")
>>> lst.append("e")
>>> lst.append("l")
>>> lst.append("l")
>>> lst.append("o")
>>> lst.append("!")
>>> print("".join(lst))
hello!
Operations
Check the ones for the Arrays.
Best Practices
Check the ones for the Arrays.