Back
I would like to read some characters from a string and put it into other string (Like we do in C).
So my code is like below
import string import re str = "Hello World" j = 0 srr = "" for i in str: srr[j] = i #'str' object does not support item assignment j = j + 1 print (srr)
import string
import re
str = "Hello World"
j = 0
srr = ""
for i in str:
srr[j] = i #'str' object does not support item assignment
j = j + 1
print (srr)
In C the code may be
i = j = 0; while(str[i] != '\0') { srr[j++] = str [i++];}
i = j = 0;
while(str[i] != '\0')
{
srr[j++] = str [i++];
}
How can I implement the same in Python?
Strings are immutable in Python, so you can't change their characters in-place.
You can, however, do the following:
for i in str: srr += i
srr += i
31k questions
32.8k answers
501 comments
693 users