Remove leading and trailing spaces?

I find it difficult to use .strip with the following line of code.

Thanks for the help.

f.write(re.split("Tech ID:|Name:|Account #:",line)[-1]) 
+52
python
May 04 '12 at 5:56 a.m.
source share
2 answers

You can use strip () to remove trailing and leading spaces.

 >>> s = ' abd cde ' >>> s.strip() 'abd cde' 

Note: interior spaces are saved.

+132
May 4 '12 at 6:10
source share
— -

Expand your single liner over several lines. Then it becomes easy:

 f.write(re.split("Tech ID:|Name:|Account #:",line)[-1]) parts = re.split("Tech ID:|Name:|Account #:",line) wanted_part = parts[-1] wanted_part_stripped = wanted_part.strip() f.write(wanted_part_stripped) 
+4
May 04 '12 at 6:03 a.m.
source share



All Articles