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@416 | 9 | print s.find("w") # 6 |
paul@416 | 10 | print s.find("w", 7) # -1 |
paul@416 | 11 | print s.find("w", 0, 6) # -1 |
paul@416 | 12 | print s.index("o") # 4 |
paul@416 | 13 | |
paul@416 | 14 | try: |
paul@416 | 15 | print s.index("p") # should raise an exception |
paul@416 | 16 | except ValueError, exc: |
paul@416 | 17 | print 's.index("p"): value is not appropriate', exc.value |
paul@416 | 18 | |
paul@416 | 19 | print s.startswith("Hello") # True |
paul@416 | 20 | print s.startswith("world") # False |
paul@416 | 21 | print s.endswith("world!") # True |
paul@416 | 22 | print s.endswith("Hello") # False |
paul@292 | 23 | |
paul@292 | 24 | s2 = "Hello worlds!" |
paul@292 | 25 | print s2 # Hello worlds! |
paul@378 | 26 | print len(s2) # 13 |
paul@292 | 27 | print s < s2 # True |
paul@292 | 28 | print s <= s2 # True |
paul@292 | 29 | print s == s2 # False |
paul@292 | 30 | print s != s2 # True |
paul@292 | 31 | print s >= s2 # False |
paul@292 | 32 | print s > s2 # False |
paul@292 | 33 | |
paul@292 | 34 | print s[0] # H |
paul@292 | 35 | print s[-1] # ! |
paul@296 | 36 | |
paul@296 | 37 | print ord(s[0]) # 72 |
paul@296 | 38 | |
paul@296 | 39 | try: |
paul@296 | 40 | print ord(s) # should raise an exception |
paul@296 | 41 | except ValueError, exc: |
paul@296 | 42 | print "ord(s): value is not appropriate", exc.value |
paul@300 | 43 | |
paul@342 | 44 | l = ["Hello", "world!"] |
paul@342 | 45 | s3 = " ".join(l) |
paul@342 | 46 | print s3 # Hello world! |
paul@378 | 47 | print len(s3) # 12 |
paul@342 | 48 | |
paul@342 | 49 | s4 = "".join(l) |
paul@342 | 50 | print s4 # Helloworld! |
paul@378 | 51 | print len(s4) # 11 |
paul@378 | 52 | |
paul@378 | 53 | s5 = "--".join(l) |
paul@378 | 54 | print s5 # Hello--world! |
paul@378 | 55 | print len(s5) # 13 |
paul@342 | 56 | |
paul@494 | 57 | print "# hash(s):", |
paul@300 | 58 | print hash(s) |
paul@494 | 59 | print "# hash(s2):", |
paul@342 | 60 | print hash(s2) |
paul@494 | 61 | print "# hash(s3):", |
paul@342 | 62 | print hash(s3) |
paul@494 | 63 | print "# hash(s4):", |
paul@342 | 64 | print hash(s4) |
paul@494 | 65 | print "# hash(s5):", |
paul@378 | 66 | print hash(s5) |