On another note, string indexing and capturing a substring is rather interesting in Python.
Substrings can be captured in a manner that probably seems normal to most people. The indexing is an inclusive/exclusive index (the beginning index is included, but the ending index is not).
s = "abCbA"
self.assert_(s[1:4] == "bCb")
self.assert_(s[1:4] is not "bCb")
self.assert_(s[1: ] == "bCbA")
self.assert_(s[1: ] is not "bCbA")
self.assert_(s[ :4] == "abCb")
self.assert_(s[ :4] is not "abCb")
self.assert_(s[0:5] == s)
self.assert_(s[0:5] is s)
self.assert_(s[ : ] == s)
self.assert_(s[ : ] is s)
Here's where things are slightly different. Python allows you to specify a step size in your indexing.
s = "abCbA"Stranger still, Python allows negative indexing. The strings do not, however, function as a circularly linked list. For this example an index of -7 as an ending index would be invalid.
self.assert_(s[1:4: 2] == "bb")
self.assert_(s[0:5: 2] == "aCA")
self.assert_(s[ : : 2] == "aCA")
s = "abCbA"
self.assert_(s[-1] is "A")
self.assert_(s[-4] is "b")
self.assert_(s[-5] is "a")
# self.assert_(s[-6] is "") // out of range
s = "abCbA"
self.assert_(s[-2:-5:-2] == "bb")
self.assert_(s[-1:-6:-2] == "ACa")
self.assert_(s[ : :-2] == "ACa")
No comments:
Post a Comment