0 votes
1 view
in Python by (41.8k 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
by (95.7k 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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...