[Tutor] Problem Stripping

Emile van Sebille emile at fenx.com
Fri Mar 30 19:28:48 CEST 2012


On 3/30/2012 10:09 AM leam hall said...
> 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.


... but it works as advertised...

Help on built-in function strip:

strip(...)
     S.strip([chars]) -> string or unicode

     Return a copy of the string S with leading and trailing
     whitespace removed.
     If chars is given and not None, remove characters in chars instead.
     If chars is unicode, S will be converted to unicode before stripping



If you're expecting the ":"s to be stripped out, try using replace:

Help on built-in function replace:

replace(...)
     S.replace (old, new[, count]) -> string

     Return a copy of string S with all occurrences of substring
     old replaced by new.  If the optional argument count is
     given, only the first count occurrences are replaced.


HTH,

Emile

>
>
> 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






More information about the Tutor mailing list