eschneide...@comcast.net wrote: > I'm on chapter 9 of this guide to python: > http://inventwithpython.com/chapter9.html but I don't quite understand > why line 79 is what it is (blanks = blanks[:i] + secretWord[i] + > blanks[i+1:]). I particularly don't get the [i+1:] part. Any additional > information and help would be greatly appreciated!
When you have a list with and want to replace the third item with something else you can do it like this: >>> items = ["r", "e", "e", "d"] >>> items[2] = "a" >>> items ['r', 'e', 'a', 'd'] With a string that is not possible because strings cannot be modified (they are "immutable"): >>> s = "reed" >>> s[2] = "a" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment You could try replace() to build a new string >>> s.replace("e", "a") 'raad' but that replaces all occurrences. So to manually replace th third character you take the first two characters of the original >>> s[:2] 're' then the characters after the third >>> s[3:] 'd' and finally build the new string by putting the new character (or string) in between: >>> s[:2] + "a" + s[3:] 'read' A function to replace the n-th character would then look like this: >>> def replace_nth_char(s, n, replacement): ... return s[:n] + replacement + s[n+1:] ... >>> replace_nth_char("read", 2, "e") 'reed' -- http://mail.python.org/mailman/listinfo/python-list