• Register
1 vote
49 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,240 points

Related questions

0 votes
1 answer 12 views
12 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 5.5k points
0 votes
1 answer 26 views
26 views
Problem: Hello Programmers! I am new to the world of programming and I am working with arrays. I am trying to shuffle the elements of an array by using random.shuffle() method. I wrote the following program: import random Flowers = ["Lilies", "Roses", "Jasmine" ... ] TypeError: 'range' object does not support item assignment I have no idea what is causing this error. Any help regarding the issue??
asked Dec 24, 2020 Code Learner 5.5k points
0 votes
1 answer 11 views
11 views
Problem: ’ object does not support item assignment occurs when you attempt to alter or modify a character in a string using assignment operator. The string is an immutable object which can’t be changed. If you attempt to change the content of a string, the error TypeError: object does not support item assignment will be thrown.
asked Dec 2, 2020 Mashhoodch 5.7k points
1 vote
1 answer 220 views
220 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, 2020 samhaz 5k points
0 votes
1 answer 52 views
1 vote
2 answers 1.6K views
1.6K 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 3 views
3 views
Problem: On the last line, I got this error: builtins.TypeError: must be str, not bytes File "C:\PythonExamples\XmlReportGeneratorExample.py", line 29, in <module> doc.write(outFile) File "c:\Python32\Lib\site-packages\lxml\etree.pyd", line 1853, in lxml.etree._ElementTree.write (src/ ... etree.c:89527) I've installed Python 3.2, and I've installed lxml-2.3.win32-py3.2.exe. On Python 2.7 it works.
asked Jan 9 Mashhoodch 5.7k points
0 votes
1 answer 52 views
52 views
I was looking for a way to run an external process from python script and print its stdout messages during the execution. The code below works, but prints no stdout output during runtime. When it exits I am getting the following error: sys.stdout.write(nextline) TypeError:must ... .stdout.write(nextline) sys.stdout.flush() output = p.communicate()[0] exitCode = p.returncode I am using python 3.3.2
asked Oct 24, 2020 psandprop 2.4k points