2016-12-12 | Paul Boddie | file changeset files shortlog | Fixed string slicing to produce strings, changing sequences to not provide list-based slicing to all descendant classes. Changed the native substring function to use start, end and step information, adding step support. Changed the names in the native string module to reflect __data__ attribute usage. Added tests of string slicing. |
paul@292 | 1 | s = "Hello" |
paul@292 | 2 | s += " world!" |
paul@292 | 3 | print s # Hello world! |
paul@378 | 4 | print len(s) # 12 |
paul@384 | 5 | print s[:5] # Hello |
paul@384 | 6 | print s[5:] # world! |
paul@384 | 7 | print s[1:10:2] # el ol |
paul@384 | 8 | print s[10:1:-2] # drwol |
paul@292 | 9 | |
paul@292 | 10 | s2 = "Hello worlds!" |
paul@292 | 11 | print s2 # Hello worlds! |
paul@378 | 12 | print len(s2) # 13 |
paul@292 | 13 | print s < s2 # True |
paul@292 | 14 | print s <= s2 # True |
paul@292 | 15 | print s == s2 # False |
paul@292 | 16 | print s != s2 # True |
paul@292 | 17 | print s >= s2 # False |
paul@292 | 18 | print s > s2 # False |
paul@292 | 19 | |
paul@292 | 20 | print s[0] # H |
paul@292 | 21 | print s[-1] # ! |
paul@296 | 22 | |
paul@296 | 23 | print ord(s[0]) # 72 |
paul@296 | 24 | |
paul@296 | 25 | try: |
paul@296 | 26 | print ord(s) # should raise an exception |
paul@296 | 27 | except ValueError, exc: |
paul@296 | 28 | print "ord(s): value is not appropriate", exc.value |
paul@300 | 29 | |
paul@342 | 30 | l = ["Hello", "world!"] |
paul@342 | 31 | s3 = " ".join(l) |
paul@342 | 32 | print s3 # Hello world! |
paul@378 | 33 | print len(s3) # 12 |
paul@342 | 34 | |
paul@342 | 35 | s4 = "".join(l) |
paul@342 | 36 | print s4 # Helloworld! |
paul@378 | 37 | print len(s4) # 11 |
paul@378 | 38 | |
paul@378 | 39 | s5 = "--".join(l) |
paul@378 | 40 | print s5 # Hello--world! |
paul@378 | 41 | print len(s5) # 13 |
paul@342 | 42 | |
paul@300 | 43 | print hash(s) |
paul@342 | 44 | print hash(s2) |
paul@342 | 45 | print hash(s3) |
paul@342 | 46 | print hash(s4) |
paul@378 | 47 | print hash(s5) |