''str' object does not support item assignment
I would like to read some characters from a string s1 and put it into another string s2.
However, assigning to s2[j] gives an error:
s2[j] = s1[i]
# TypeError: 'str' object does not support item assignment
In C, this works:
int i = j = 0;
while (s1[i] != '\0')
s2[j++] = s1[i++];
My attempt in Python:
s1 = "Hello World"
s2 = ""
j = 0
for i in range(len(s1)):
s2[j] = s1[i]
j = j + 1
Solution 1:[1]
The other answers are correct, but you can, of course, do something like:
>>> str1 = "mystring"
>>> list1 = list(str1)
>>> list1[5] = 'u'
>>> str1 = ''.join(list1)
>>> print(str1)
mystrung
>>> type(str1)
<type 'str'>
if you really want to.
Solution 2:[2]
assigning to
s2[j]gives an error
Strings are immutable so what you've done in C won't be possible in Python. Instead, you'll have to create a new string.
I would like to read some characters from a string and put it into other string.
Use a slice:
>>> s1 = 'Hello world!!'
>>> s2 = s1[6:12]
>>> print(s2)
world!
Solution 3:[3]
Strings in Python are immutable (you cannot change them inplace).
What you are trying to do can be done in many ways:
Copy the string:
foo = 'Hello'
bar = foo
Create a new string by joining all characters of the old string:
new_string = ''.join(c for c in oldstring)
Slice and copy:
new_string = oldstring[:]
Solution 4:[4]
Other answers convert the string to a list or construct a new string character by character. These methods can be costly, especially for large strings. Instead, we can use slicing to get the parts of the string before and after the character that is changed, and combine those with the new character.
Here I modify the example code from Crowman's answer to replace a single character in the string using string slicing instead of conversion to a list.
>>> str1 = "mystring"
>>> pos = 5
>>> new_char = 'u'
>>> str2 = str1[:pos] + new_char + str1[pos+1:]
>>> print(str2)
mystrung
>>> type(str2)
<class 'str'>
Solution 5:[5]
Another approach if you wanted to swap out a specific character for another character:
def swap(input_string):
if len(input_string) == 0:
return input_string
if input_string[0] == "x":
return "y" + swap(input_string[1:])
else:
return input_string[0] + swap(input_string[1:])
Solution 6:[6]
Performant methods
If you are frequently performing index replacements, a more performant and memory-compact method is to convert to a different data structure. Then, convert back to string when you're done.
list:
Easiest and simplest:
s = "TEXT"
s = list(s)
s[1] = "_"
s = "".join(s)
bytearray (ASCII):
This method uses less memory. The memory is also contiguous, though that doesn't really matter much in Python if you're doing single-element random access anyways:
ENC_TYPE = "ascii"
s = "TEXT"
s = bytearray(s, ENC_TYPE)
s[1] = ord("_")
s = s.decode(ENC_TYPE)
bytearray (UTF-32):
More generally, for characters outside the base ASCII set, I recommend using UTF-32 (or sometimes UTF-16), which will ensure alignment for random access:
ENC_TYPE = "utf32"
ENC_WIDTH = 4
def replace(s, i, replacement):
start = ENC_WIDTH * (i + 1)
end = ENC_WIDTH * (i + 2)
s[start:end] = bytearray(replacement, ENC_TYPE)[ENC_WIDTH:]
s = "TEXT HI ? RA ? GA ? NA ? DONE"
s = bytearray(s, ENC_TYPE)
# Performs s[1] = "_"
replace(s, 1, "_")
s = s.decode(ENC_TYPE)
Though this method may be more memory-compact than using list, it does require many more operations.
Solution 7:[7]
In my genius rookie situation I tried to add keys / values to a dictionary incorrectly. like so.
mydict = {}
mydict = mydict['my_key'] = 'my_value'
Where it should be.
mydict['my_key'] = 'my_value'
Solution 8:[8]
The 'str' is an immutable data type. Therefore str type object doesn't support item assignment.
s1 = "Hello World"
s2 = ['']*len(s1)
j = 0
for i in range(len(s1)):
s2[j]=s1[i]
j = j + 1
print(''.join(s2)) # Hello World
Solution 9:[9]
How about this solution:
str="Hello World" (as stated in problem) srr = str+ ""
Solution 10:[10]
Hi you should try the string split method:
i = "Hello world"
output = i.split()
j = 'is not enough'
print 'The', output[1], j
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
