|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
|
|
#1
|
|||
|
|||
|
Stripping
I'm a little bit confused with strip function in module STRING sometime it works sometime not.For example
Code:
>> a = 'myname/is' >> print string.strip(a,'/') >> myname/is so what's wrong? |
|
#2
|
|||
|
|||
|
How is that not working?
|
|
#3
|
|||
|
|||
|
Code:
>>> import string
>>> help(string.strip)
Help on function strip in module string:
strip(s, chars=None)
strip(s [,chars]) -> string
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.
>>> string.strip('myname/is', '/')
'myname/is'
>>> string.strip('///myname/is///', '/')
'myname/is'
>>>
Dave - The Developers' Coach |
|
#4
|
|||
|
|||
|
BTW, the functions in the string module are the old way of doing it. Unless your code needs to be backwards compatible with Python 1.5.2 and earlier, then the preferred way is to use the methods on the string object itself:
Code:
>>> '///myname/is///'.strip('/')
'myname/is'
>>>
Dave - The Developers' Coach |
|
#5
|
||||
|
||||
|
If you want to replace all occurances of particular strings/chars then you should look at the replace() method - very handy, i seem to use it when ever i do any string manipulation.
Have fun, Mark. |
|
#6
|
|||
|
|||
|
Quote:
Yeah that one is COOL .. screw strip function. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Stripping |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|