hi
can someone explain strip() for these : -
-
-
-
>>x='www.example.com'
- x.strip('cmowz.')
-
-
-
-
-
'example'
-
when i did this: -
-
-
-
>>x = 'abcd,words.words'
- x.strip(',.')
-
-
-
-
-
'abcd,words.words'
-
it does not strip off "," and "." .Why is this so?
thanks 6 2326 ei***********@yahoo.com wrote:
hi
can someone explain strip() for these : -
-
-
>>>>x='www.example.com'
- x.strip('cmowz.')
-
-
-
-
-
'example'
-
when i did this: -
-
-
>>>>x = 'abcd,words.words'
- x.strip(',.')
-
-
-
-
-
'abcd,words.words'
-
it does not strip off "," and "." .Why is this so?
thanks
strip strips from the ends.
pyx = '...,.,abcd,words.words,,,,.,.,.,.,'
pyx.strip(',.')
'abcd,words.words'
James
On Fri, 26 Jan 2007 21:33:47 -0800, eight02645999 wrote:
hi
can someone explain strip() for these : -
-
-
>>>x='www.example.com'
- x.strip('cmowz.')
-
-
-
-
'example'
-
when i did this: -
-
-
>>>x = 'abcd,words.words'
- x.strip(',.')
-
-
-
-
'abcd,words.words'
-
it does not strip off "," and "." .Why is this so?
thanks
Fascinating...
It gets weirder:
>>x.strip('s')
'abcd,words.word'
Why strip only the final s, not the earlier one?
>>x.strip('w')
'abcd,words.words'
>>x.strip('o')
'abcd,words.words'
>>x.strip('r')
'abcd,words.words'
Strips nothing.
>>x.strip('ba')
'cd,words.words'
Strips correctly.
>>x.strip('bwa')
'cd,words.words'
Strips the a and b but not the w.
>>x.strip('bwas')
'cd,words.word'
....and only one of the S's.
>>y = "bwas" y.strip('bwas')
''
>>y = "bwasxyz" y.strip('bwas')
'xyz'
And yet these work.
You know, I'm starting to think there may be a bug in the strip method...
either that or the documentation should say:
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 none, some or all characters in
chars instead. If chars is unicode, S will be converted to unicode
before stripping
*wink*
--
Steven.
Steven D'Aprano wrote:
On Fri, 26 Jan 2007 21:33:47 -0800, eight02645999 wrote:
>>hi can someone explain strip() for these : -
-
>>>>>x='www.example.com'
- >x.strip('cmowz.')
-
-
-
- 'example'
when i did this: -
-
>>>>>x = 'abcd,words.words'
- >x.strip(',.')
-
-
-
- 'abcd,words.words'
it does not strip off "," and "." .Why is this so? thanks
Fascinating...
It gets weirder:
>>>>x.strip('s')
'abcd,words.word'
Why strip only the final s, not the earlier one?
>>>>x.strip('w')
'abcd,words.words'
>>>>x.strip('o')
'abcd,words.words'
>>>>x.strip('r')
'abcd,words.words'
Strips nothing.
>>>>x.strip('ba')
'cd,words.words'
Strips correctly.
>>>>x.strip('bwa')
'cd,words.words'
Strips the a and b but not the w.
>>>>x.strip('bwas')
'cd,words.word'
...and only one of the S's.
>>>>y = "bwas" y.strip('bwas')
''
>>>>y = "bwasxyz" y.strip('bwas')
'xyz'
And yet these work.
You know, I'm starting to think there may be a bug in the strip method...
either that or the documentation should say:
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 none, some or all characters in
chars instead. If chars is unicode, S will be converted to unicode
before stripping
*wink*
At the risk of appearing that I didn't notice your *wink*, the operative
words in the docs would be "leading" and "trailing". But somehow I think
you knew that.
James
On Jan 27, 4:33 pm, eight02645...@yahoo.com wrote:
hi
can someone explain strip() for these : - >>x='www.example.com'
-
-
-
>x.strip('cmowz.')'example'
-
-
-
-
According to the documentation (with emphasis added):
"""Return a copy of the string with the *leading* and *trailing*
characters removed."""
Looking at the leading edge: "w" is in the set to strip, so it is
removed. Same applies to another 2 "w"s and a dot. "e" is not in the
strip set so it stops.
At the trailing edge: "m", "o", "c" and dot are removed, then it stops
because there's another "e".
when i did this: - >>x = 'abcd,words.words'
-
-
-
>x.strip(',.')'abcd,words.words'
-
-
-
-
it does not strip off "," and "." .Why is this so?
The comma and dot in your example are neither leading nor trailing.
The leading "a" is not in the set to strip, so nothing happens at the
front door.
The trailing "s" is not in the set to strip, so there's no back door
action either.
HTH,
John ei***********@yahoo.com a écrit :
hi
can someone explain strip() for these : -
-
-
>>>>x='www.example.com'
- x.strip('cmowz.')
-
-
-
-
-
'example'
-
when i did this: -
-
-
>>>>x = 'abcd,words.words'
- x.strip(',.')
-
-
-
-
-
'abcd,words.words'
-
it does not strip off "," and "." .Why is this so?
Probably because the Fine Manual(tm) says that str.strip() removes
heading and trailing chars ?
"""
bruno@bibi ~ $ python
Python 2.4.1 (#1, Jul 23 2005, 00:37:37)
[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on
linux2
Type "help", "copyright", "credits" or "license" for more information.
>>help(''.strip)
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
"""
You may want to try str.replace() instead:
"""
>>help(''.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.
>>'abcd,words.words'.replace(',', '').replace('.', '')
'abcdwordswords'
"""
thanks
HTH
On 26 Jan 2007 21:33:47 -0800, ei***********@yahoo.com wrote:
>hi can someone explain strip() for these : -
-
-
>>>x='www.example.com'
- x.strip('cmowz.')
-
-
-
-
'example'
when i did this: -
-
-
>>>x = 'abcd,words.words'
- x.strip(',.')
-
-
-
-
'abcd,words.words'
it does not strip off "," and "." .Why is this so? thanks
If you only have a couple of characters to deal with then use
replace(). Otherwise use string.translate() :
>>import string x = 'abcd,words.words' transform = string.maketrans(',.','..') x = string.translate(x, transform) x = x.replace('.','') x
'abcdwordswords''
Dan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: jcf |
last post by:
I need to do the following: Our application builds information from a
string that is stored in a buffer that can be any of the following
formats: ABCD0102, ABCDEF0102, AB*CD*01*02, AB*CDEF*01*02....
|
by: Smoke |
last post by:
Im testing the new June CTP release of VS2005 and each time i try to use the Status Strip or Tool Strip control at design time i get
and object reference not set to an instance of an object error,...
|
by: dan.j.weber |
last post by:
I'm using Python 2.3.5 and when I type the following in the interactive
prompt I see that strip() is not working as advertised:
>>>s = 'p p:p'
>>>s.strip(' :')
'p p:p'
Is this just me or...
|
by: rtilley |
last post by:
s = ' qazwsx '
# How are these different?
print s.strip()
print str.strip(s)
Do string objects all have the attribute strip()? If so, why is
str.strip() needed? Really, I'm just curious......
|
by: Adam Honek |
last post by:
Hi all,
The tool strip control is set to flow layout, no autosize and to anchor top,
left.
When the form is maximized all the icons and text fit in one line across the
screen.
The menu...
|
by: amattie |
last post by:
Does anyone have any idea on how I can strip the extra whitespace in
the XML that shows up when I receive a response from an ASP.NET 2.0
webservice? This has been discussed before, but no one has...
|
by: Colin J. Williams |
last post by:
The Library Reference has
strip( )
Return a copy of the string with the
leading and trailing characters removed.
The chars argument is a string
specifying the set of characters to be
removed....
|
by: Christoph Zwerschke |
last post by:
In Python programs, you will quite frequently find code like the
following for removing a certain prefix from a string:
if url.startswith('http://'):
url = url
Similarly for stripping...
|
by: Poppy |
last post by:
I'm using versions 2.5.2 and 2.5.1 of python and have encountered a
potential bug. Not sure if I'm misunderstanding the usage of the strip
function but here's my example.
var = "detail.xml"...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |