Lichen

tests/string.py

1033:4da5e97181b6
5 months ago Paul Boddie Merged changes from the trailing-data branch. value-replacement
     1 s = "Hello"     2 s += " world!"     3 print s                     # Hello world!     4 print len(s)                # 12     5 print s[:5]                 # Hello     6 print s[5:]                 #  world!     7 print s[1:10:2]             # el ol     8 print s[10:1:-2]            # drwol     9 print s[11:]                # !    10 print s[:-11]               # H    11 print s[12:]                #    12 print s[:-12]               #    13 print s.find("w")           # 6    14 print s.find("w", 7)        # -1    15 print s.find("w", 0, 6)     # -1    16 print s.index("o")          # 4    17 print s.rfind("o")          # 7    18 print s.rfind("o", 7)       # 7    19 print s.rfind("o", 8)       # -1    20 print s.rfind("o", 0, 7)    # 4    21     22 try:    23     print s.index("p")      # should raise an exception    24 except ValueError, exc:    25     print 's.index("p"): value is not appropriate', repr(exc.value)    26     27 print s.startswith("Hello") # True    28 print s.startswith("world") # False    29 print s.endswith("world!")  # True    30 print s.endswith("Hello")   # False    31     32 s2 = "Hello worlds!"    33 print s2                    # Hello worlds!    34 print len(s2)               # 13    35 print s < s2                # True    36 print s <= s2               # True    37 print s == s2               # False    38 print s != s2               # True    39 print s >= s2               # False    40 print s > s2                # False    41     42 print s[0]                  # H    43 print s[-1]                 # !    44     45 print ord(s[0])             # 72    46     47 try:    48     print ord(s)            # should raise an exception    49 except ValueError, exc:    50     print "ord(s): value is not appropriate", repr(exc.value)    51     52 print chr(72)               # H    53 print repr(chr(0))          # "\x00"    54     55 try:    56     print repr(chr(-1))     # should raise an exception    57 except ValueError, exc:    58     print "chr(-1): value is not appropriate", exc.value    59     60 l = ["Hello", "world!"]    61 s3 = " ".join(l)    62 print s3                    # Hello world!    63 print len(s3)               # 12    64     65 s4 = "".join(l)    66 print s4                    # Helloworld!    67 print len(s4)               # 11    68     69 s5 = "--".join(l)    70 print s5                    # Hello--world!    71 print len(s5)               # 13    72     73 print "# hash(s):",    74 print hash(s)    75 print "# hash(s2):",    76 print hash(s2)    77 print "# hash(s3):",    78 print hash(s3)    79 print "# hash(s4):",    80 print hash(s4)    81 print "# hash(s5):",    82 print hash(s5)    83     84 # Test multiplication of strings.    85     86 s6 = "abc"    87 print s6 * -1               #    88 print s6 * 0                #    89 print s6 * 1                # abc    90 print s6 * 2                # abcabc    91 print -1 * s6               #    92 print 0 * s6                #    93 print 1 * s6                # abc    94 print 2 * s6                # abcabc    95     96 # Test splitting of strings.    97     98 s7 = "Hello...\n  world,\n  planet,\n  globe."    99 print s7.split()            # ["Hello...", "world,", "planet,", "globe."]   100 print s7.split(maxsplit=2)  # ["Hello...", "world,", "planet,\n  globe."]   101 print s7.split("\n")        # ["Hello...", "  world,", "  planet,", "  globe."]   102    103 print "RGB(%r, %r, %r)".split("%") # ["RGB(", "r, ", "r, ", "r)"]   104    105 # NOTE: To test rsplit once list.insert is implemented.   106    107 # Test stripping of strings.   108    109 s8 = "  \nHello world\n  "   110 print repr(s8.strip())      # "Hello world"   111 print repr(s8.lstrip())     # "Hello world\n  "   112 print repr(s8.rstrip())     # "  \nHello world"   113    114 s9 = "xyzHello worldXYZ"   115 print repr(s9.strip("xyYZ"))    # "zHello worldX"   116 print repr(s9.lstrip("xyYZ"))   # "zHello worldXYZ"   117 print repr(s9.rstrip("xyYZ"))   # "xyzHello worldX"   118    119 # Test quoting of strings.   120    121 print repr('?\n?\r?\t"')    # "\xe6\n\xf8\r\xe5\t\""   122    123 # Test formatting of strings.   124    125 print "RGB(%r, %r, %r)" % (1, 2, 3)