I would like to read some characters from a string s1
and put it into another string s2
.
However, assigning to s2[j]
gives an error:
s2[j] = s1[i]
# TypeError: 'str' object does not support item assignment
In C, this works:
int i = j = 0;
while (s1[i] != '\0')
s2[j++] = s1[i++];
My attempt in Python:
s1 = "Hello World"
s2 = ""
j = 0
for i in range(len(s1)):
s2[j] = s1[i]
j = j + 1
Best Answer
The other answers are correct, but you can, of course, do something like:
if you really want to.