0 votes
1 view
ago in Python by (19.5k points)

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)

In C the code may be

i = j = 0; 

while(str[i] != '\0') 

srr[j++] = str [i++];

}

How can I implement the same in Python?

1 Answer

0 votes
ago by (46.1k points)

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

...