[Tutor] Problem Stripping

Joel Goldstick joel.goldstick at gmail.com
Fri Mar 30 19:28:30 CEST 2012


On Fri, Mar 30, 2012 at 1:25 PM, Joel Goldstick
<joel.goldstick at gmail.com> wrote:
> On Fri, Mar 30, 2012 at 1:09 PM, leam hall <leamhall at gmail.com> wrote:
>> Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
>> but it doesn't seem to work like I thought.
>>
>>
>> res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
>> uname = res.stdout.read().strip()
>>
>>>>> uname
>> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
>> i686 i686 i386 GNU/Linux'
>>
>>>>> uname.strip(':')
>> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
>> i686 i686 i386 GNU/Linux'
>>
>>>>> 'www.example.com'.strip('cmowz.')
>> 'example'
>>
>> Thoughts?
>>
>> Leam
>> --
>> Mind on a Mission <http://leamhall.blogspot.com/>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
> str.strip() removes characters from begining and end of the string --
> Not any in between.  Notice
>
>>>> example = "www.example.com".strip("wcomx")
>>>> example
> '.example.'
>>>>
>
> The x remains


You could do list comprehension
>
>>> n = "".join([x for x in "this has : some : colons" if x not in ':'])
>>> n
'this has  some  colons'
>
-- 
Joel Goldstick


More information about the Tutor mailing list