t432.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. def helper(got,expect):
  2. if got == expect: print True
  3. else: print False, expect, got
  4. print "\nstr.capitalize"
  5. helper('hello world'.capitalize(),'Hello world')
  6. helper('HELLO WorlD'.capitalize(),'Hello world')
  7. print "\nstr.center"
  8. helper('12345'.center(7),' 12345 ')
  9. helper('12345'.center(8),' 12345 ')
  10. print "\nstr.count"
  11. helper('abcd abcba '.count('abc'),2)
  12. helper('aaaaaaaaaaa '.count('aa'),5)
  13. print "\nstr.find"
  14. helper('hello world'.find('l'),2)
  15. helper('hello world'.find('X'),-1)
  16. helper('hello world'.find('l',3),3)
  17. print "\nstr.index"
  18. helper('hello world'.index('l'),2)
  19. helper('hello world'.index('l',3),3)
  20. print "\nstr.isdigit"
  21. helper('hello'.isdigit(),False)
  22. helper('1234'.isdigit(),True)
  23. helper(''.isdigit(),False)
  24. helper('123.45'.isdigit(),False)
  25. print "\nstr.isalpha"
  26. helper('ABCabc'.isalpha(),True)
  27. helper('abc123'.isalpha(),False)
  28. helper('ABC abc'.isalpha(),False)
  29. helper(''.isalpha(),False)
  30. print "\nstr.isalnum"
  31. helper('ABCabc'.isalnum(),True)
  32. helper('abc123'.isalnum(),True)
  33. helper('ABC abc'.isalnum(),False)
  34. helper(''.isalnum(),False)
  35. print "\nstr.islower"
  36. helper('abc'.islower(),True)
  37. helper('abc123'.islower(),True)
  38. helper('ABC abc'.islower(),False)
  39. helper(''.islower(),False)
  40. print "\nstr.isupper"
  41. helper('ABC'.isupper(),True)
  42. helper('ABC123'.isupper(),True)
  43. helper('ABC abc'.isupper(),False)
  44. helper(''.isupper(),False)
  45. print "\nstr.isnumeric"
  46. helper('123'.isnumeric(),True)
  47. helper('abc123'.isnumeric(),False)
  48. helper('1 2 3'.isnumeric(),False)
  49. helper('123.4'.isnumeric(),False)
  50. helper(''.isnumeric(),False)
  51. print "\nstr.join"
  52. helper('-'.join('1234'),'1-2-3-4')
  53. helper('-'.join(('1','2','3','4')),'1-2-3-4')
  54. helper('-'.join(['1','2','3','4']),'1-2-3-4')
  55. print "\nstr.ljust"
  56. helper('12345'.ljust(8),'12345 ')
  57. helper(' 12345'.ljust(8),' 12345')
  58. print "\nstr.lower"
  59. helper("HELLO".lower(),'hello')
  60. helper("Hello woRLd!".lower(),'hello world!')
  61. helper("hello".lower(),'hello')
  62. helper(''.lower(),'')
  63. print "\nstr.lstrip"
  64. helper(' hello'.lstrip(),'hello')
  65. helper(' '.lstrip(),'')
  66. print "\nstr.partition"
  67. helper('hello'.partition('h'),('','h','ello'))
  68. helper('hello'.partition('l'),('he','l','lo'))
  69. helper('hello'.partition('o'),('hell','o',''))
  70. helper('hello'.partition('x'),('hello','',''))
  71. print "\nstr.replace"
  72. helper('hello'.replace('l','L'),'heLLo')
  73. helper('hello wOrld!'.replace('o',''),'hell wOrld!')
  74. helper(''.replace('','hello'),'hello')
  75. helper('hello'.replace('','!'),'!h!e!l!l!o!')
  76. helper('abcabcaaaabc'.replace('abc','123'),'123123aaa123')
  77. print "\nstr.rfind"
  78. helper('hello world'.rfind('l'),9)
  79. helper('hello world'.rfind('X'),-1)
  80. helper('hello world'.rfind('l',3),9)
  81. print "\nstr.rindex"
  82. helper('hello world'.rindex('l'),9)
  83. helper('hello world'.rindex('l',3),9)
  84. print "\nstr.rjust"
  85. helper('12345'.rjust(8),' 12345')
  86. helper('12345 '.rjust(8),'12345 ')
  87. print "\nstr.rpartition"
  88. helper('hello'.rpartition('h'),('','h','ello'))
  89. helper('hello'.rpartition('l'),('hel','l','o'))
  90. helper('hello'.rpartition('o'),('hell','o',''))
  91. helper('hello'.rpartition('x'),('','','hello'))
  92. print "\nstr.rstrip"
  93. helper('hello '.rstrip(),'hello')
  94. helper(' '.rstrip(),'')
  95. print "\nstr.split"
  96. helper(''.split(),[])
  97. helper(''.split(None),[])
  98. helper('hello'.split(),['hello'])
  99. helper('hello'.split(None),['hello'])
  100. helper('hello world ! '.split(),['hello','world','!'])
  101. helper(''.split('a'),[''])
  102. helper(''.split('a',1),[''])
  103. helper('hello'.split('l'),['he','','o'])
  104. helper('hello'.split('l',1),['he','lo'])
  105. print "\nstr.strip"
  106. helper(' hello '.strip(),'hello')
  107. helper(' '.strip(),'')
  108. print "\nstr.upper"
  109. helper('hello'.upper(),'HELLO')
  110. helper("Hello woRLd!".upper(),'HELLO WORLD!')
  111. helper("HELLO".upper(),'HELLO')
  112. helper(''.upper(),'')