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!
(You're using googlegroups, which is painfully buggy. Just an alert that some folks won't even see a message from googlegroups, because they filter it out in protest. If you join this mailing list with more conventional means, you can avoid several problems.) The syntax you're describing is called a slice. it works on strings, and on lists, and some other places. For strings in particular, because they are immutable, something that ought to be easy becomes a strange-looking slice expression. it's worth taking the trouble to figure out the patterns of those, so you can just recognize them without too much thought. And of course, asking here is one good first step for that. A slice can specify up to 3 numeric values, which could be labeled as start, end, and step, just like range. However, they're separated by colons, rather than commas. Go to the debugger (by just typing python at the command prompt, without a script), and play a bit. "abcdefg"[:3] is analogous to range(3), which is equivalent to range(0,3). Those ranges produce a list containing 0, 1 and 2. So the string slice produces the characters at those 3 positions. In other words, "abc" (Remember Python is 99.9% zero-based). (If on Python 3.x, use list(range(qqq)) to expand it out and actually see the elements as a list) "abcdefg"[4:] is analogous to range(4, 7) and produces 'efg' Notice that character 3 is not in either string. The letter 'd' is missing. So this particular pattern: xxx[:i] + other + xxx[i+1:] lets you replace the ith character with whatever's in "other". if it's a single character, the net effect is just replacing the ith character with a different one. Does that help? -- DaveA -- http://mail.python.org/mailman/listinfo/python-list