1 'Take a string and remove all leading and trailing whitespace'
2
3 def newStrip(str):
4 'delete blanks around a string'
5 _end = len(str)
6 _start = 0
7
8 # delete the blanks at the beginning of the string
9 for i in range(_end):
10 if str != ' ':
11 _start = i
12 break
13 else:
14 print 'invalid: The string is a blank string.' # if the string is a
15 _blank = True # blank string
16
17
18 # delete the blanks in the end of the string
19 for i in range(-1, _start - _end, -1):
20 if str != ' ':
21 _end = _end + i
22 break
23 if not _blank:
24 print '-' + str[_start: _end] + '-' # print '-' make sure the function work
25
26 # test
27 if __name__ == '__main__':
28 newStrip(raw_input('Enter a string: '))