1
0

04_str.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. assert 'testing' == 'test' + 'ing'
  2. assert 'testing' != 'test' + 'ing2'
  3. assert 'testing' < 'test' + 'ing2'
  4. assert 'testing5' > 'test' + 'ing1'
  5. # test + *=
  6. assert 'abc' + 'def' == 'abcdef'
  7. assert 'abc' * 3 == 'abcabcabc'
  8. a = ''
  9. b = 'test'
  10. c ='test'
  11. assert len(a) == 0
  12. assert len(b) == 4
  13. assert b == c
  14. # upper and lower not work for utf-8
  15. # assert ''.lower() == '' and ''.upper() == ''
  16. # assert 'already+lower '.lower() == 'already+lower '
  17. # assert 'ALREADY+UPPER '.upper() == 'ALREADY+UPPER '
  18. # assert 'tEST+InG'.lower() == 'test+ing'
  19. # assert 'tEST+InG'.upper() == 'TEST+ING'
  20. s = "football"
  21. q = "abcd"
  22. r = "zoo"
  23. t = "this is string example....wow!!!"
  24. assert s[0] == 'f'
  25. assert s[1:4] == 'oot'
  26. assert s[:-1] == 'footbal'
  27. assert s[:10] == 'football'
  28. assert s[-3] == 'a'
  29. assert t[-5:] == 'ow!!!'
  30. assert t[3:-3] == 's is string example....wow'
  31. assert s > q;assert s < r
  32. assert s.replace("foo","ball") == "balltball"
  33. assert s.startswith('f') == True;assert s.endswith('o') == False
  34. assert t.startswith('this') == True;
  35. assert t.split('w') == ['this is string example....', 'o', '!!!']
  36. assert "a,b,c".split(',') == ['a', 'b', 'c']
  37. assert 'a,'.split(',') == ['a', '']
  38. assert 'foo!!bar!!baz'.split('!!') == ['foo', 'bar', 'baz']
  39. t = "*****this is **string** example....wow!!!*****"
  40. s = "123abcrunoob321"
  41. assert t.strip( '*' ) == "this is **string** example....wow!!!"
  42. assert s.strip( '12' ) == "3abcrunoob3"
  43. assert t.strip( '*' ) == "this is **string** example....wow!!!"
  44. assert s.strip( '12' ) == "3abcrunoob3"
  45. s = ' asd\n asd \n'
  46. assert s.strip() == 'asd\n asd'
  47. s1 = "-"
  48. s2 = ""
  49. seq = ["r","u","n","o","o","b"]
  50. assert s1.join( seq ) == "r-u-n-o-o-b"
  51. assert s2.join( seq ) == "runoob"
  52. def test(*seq):
  53. return s1.join(seq)
  54. assert test("r", "u", "n", "o", "o", "b") == "r-u-n-o-o-b"
  55. def f():
  56. for i in range(5):
  57. yield str(i)
  58. assert '|'.join(f()) == '0|1|2|3|4'
  59. num = 6
  60. assert str(num) == '6'
  61. # test Lo group names
  62. 测试 = "test"
  63. assert 测试 == "test"