Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k 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 (106k 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

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...