Programming Questions
word="somebigword" list(word) word[4:7]=list("small")I get an error as below. Why does this happen, even though the String has been converted as a list in line 2.
Traceback (most recent call last): File "<pyshell#19>", line 1, in <module> word[4:7]=list('small') TypeError: 'str' object does not support item assignmentHowever, if i do the following, it works fine. I want to understand the error in the previous case.
word=list("somebigword") list(word) word[4:7]=list("small")The output is ['s', 'o', 'm', 'e', 's', 'm', 'a', 'l', 'l', 'w', 'o', 'r', 'd']
word="somebigword"Next you say you list the word, and while this is true, you are not assigning it to the value of word itself. To do this, you would need to rephrase the code to:
word=list(word)In the second code you posted, this words because the list is actually assigned to the value of word:
word=list("somebigword")Again when you say list(word) it's not doing anything with the variable, it's simply running and returning true. The value assigned to the variable word is still the same. So in theory, you could use either of these bits of code:
word="somebigword" word=list(word) word[4:7]=list("small")
word=list("somebigword") word[4:7]=list("small")
Ninjex
answered on 12/28/13
zohaan
answered on 12/28/13