• Register
1 vote
32 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

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,240 points

Related questions

1 vote
1 answer 94 views
94 views
Problem: For the following code I got some errors. The code I wrote is: def algo(n,b,a): assert(b > 1) x = n y = 0 while x != 0: a[y] = x % b x = x / b ++y return y print (algo(1,333,666)) print (algo(11,333,666)) print (algo(111,1001,99)) ... 666)) File "tar.py", line 6, in algo a[y] = x % b TypeError: 'int' object does not support item assignment Why am I getting this error can anyone explain?
asked Apr 21 samhaz 5k points
1 vote
2 answers 746 views
746 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 LizzyM 6.1k points