row = {"1":"aaaaaaaaaaaaaaaaaaaaaaaaaaa"}
for z in range(1,8,3):
row["1"][z] = "O"
I want to change the 1,4, and 7th element of the string to "O"
row = {"1":"aaaaaaaaaaaaaaaaaaaaaaaaaaa"}
for z in range(1,8,3):
row["1"][z] = "O"
I want to change the 1,4, and 7th element of the string to "O"
Python strings are immutable. One way to accomplish what you want is to split the string into a list of characters and mutate that:
char_list = list(row["1"])
for z in range(1, 8, 3):
char_list[z] = "O"
row["1"] = "".join(char_list)
The new value of row
will be:
{'1': 'aOaaOaaOaaaaaaaaaaaaaaaaaaa'}
String are immutable in python, so you can't change it once created. To replace a value at an index, there are several ways of doing it. One easy way is to create a new string, join values before and after the index
row = {"1":"aaaaaaaaaaaaaaaaaaaaaaaaaaa"}
new_value = "o"
for z in range(1,8,3):
row["1"] = row["1"][:z] + new_value + row["1"][z+1:]
print(row["1"])
#aoaaoaaoaaaaaaaaaaaaaaaaaaa