• Register
1 vote
78 views

Problem:

I am working on python string.

import string
import re
str = "Hello"
b = 0
srr = ""
for a in str:
    srr[b] = a #'str' object does not support item assignment 
    b = b + 1
print (srr)

This is my sample code. I was trying to read some characters from a string and put it into another string like C or Java but it’s not working for my case.
Thanks. I must recognize your help.

 

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

2 votes

Solution:

Strings are immutable in Python. You can not change the characters in their place. But you are allowed to do

for a in str:
    srr += a

This program creates a new String with each iteration and stores a new String in srr. 

13 9 6
94,260 points

Related questions

1 vote
1 answer 6 views
6 views
Problem: I am unable to find out the solution, Please solve it : python str object does not support item assignment
asked Mar 19 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 191 views
1 vote
2 answers 3.1K views
3.1K views
Problem: I was trying to insert data from one of my dictionaries into my database for that I wrote the code: def all_db_inserts(dbinformation): try: rows = dbinfo['datarows'] for row in rows: field_names = ",".join(["'{0}'".format(x) for x in row.keys()] ... this error message when I try to run this(using sample data): TypeError: sequence item 0: expected string, int found Can anyone help me out?
asked Apr 5, 2020 LizzyM 6.1k points
0 votes
1 answer 4 views
4 views
Problem: I have Dataframe of "IMDB data from 2006 to 2016" which is in Kaggle site: kaggle.com/PromptCloudHQ/imdb-data . I have made it as numpy array but when I want to assign the inner product of two row of it to numpy.float64 variable, it gives me this error: sim[i][1] ... inner product it gives me exactly this type: >>chi = np.inner(vec[0],vec[0]) >>print(type(chi)) <class 'numpy.float64'>
asked 1 day ago jamuna1 30.2k points
0 votes
1 answer 7 views
7 views
Problem: Is there anyone here who know about my problem? If yes, then please help me out.
asked Apr 3 niyatiry 3.9k points
0 votes
1 answer 37 views
37 views
Problem: I get an error on the last line: TypeError: 'Tensor' object does not support item assignment It seems I can not assign to tensor, how can I fix it?
asked Mar 21 ummesalma 29.2k points
0 votes
1 answer 13 views
13 views
Problem: I am using the PIL library. I am trying to make an image look red-er, this is what I've got. However I get this error: TypeError: 'tuple' object does not support item assignment
asked Mar 16 ummesalma 29.2k points
0 votes
1 answer 22 views
22 views
Problem: How do I fix TypeError: 'Unicode object does not support item assignment&rdquo; in dictionaries. Need HELP!
asked Mar 16 ummesalma 29.2k points
0 votes
1 answer 41 views
41 views
Problem: I am new to the world of programming . I recently started learning Python. I wrote following program: def check(list): for i in range(len(list)): list[i] = list[i+1] value = list[i] print(value) input_list = eval(input("Enter the items of list")) check( ... to replace the first item of the list with the second, and so on. I have no idea why this error is occurring. Can anybody help me?
asked Dec 27, 2020 Code Learner 9.8k points