473,395 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

strip not working on strings?

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 does it not work? I want to get rid of all ' ' and
':' in the string. I've checked the doc and from what I can tell this
is what strip() is supposed to do. Thanks for any help.

Nov 22 '05 #1
5 8695
On Sun, 2005-11-13 at 13:16 -0800, da*********@gmail.com wrote:
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 does it not work? I want to get rid of all ' ' and
':' in the string. I've checked the doc and from what I can tell this
is what strip() is supposed to do. Thanks for any help.


According to my docs it says "Return a copy of the string with the
leading and trailing characters removed." There are no leading or
trailing spaces or colons in 'p p:p'.

What your probably looking for is the .replace() method.

-m

Nov 22 '05 #2
da*********@gmail.com a écrit :
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 does it not work? I want to get rid of all ' ' and
':' in the string. I've checked the doc and from what I can tell this
is what strip() is supposed to do. Thanks for any help.


strip(chars) "returns a copy of the string with leading and trailing
characters removed", that is, at the beginning and at the end of the string

You can use this to remove the specified characters :

for char in chars:
s.replace(char,'')

Pierre
Nov 22 '05 #3
In article <11**********************@g14g2000cwa.googlegroups .com>,
da*********@gmail.com wrote:
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 does it not work? I want to get rid of all ' ' and
':' in the string. I've checked the doc and from what I can tell this
is what strip() is supposed to do.


In /my/ docs, s.strip return a copy of s where all /leading/ and
/heading/ spaces are removed. s.strip(x) does the same but removing
chars of x.

So, what you're asking for by s.strip(' :') is "remove heading or
leading space or ':' chars", /not/ "remove heading or leading space or
':' chars".

If you want to get rid of ' ' and ':' anywhere in s, i think that
string.maketrans and s.translate will do the job:
import string
s = 'p p:p'
ident = string.maketrans('', '')
s.translate(ident,' :')

'ppp'

--
Jaco
Nov 22 '05 #4
In article <11**********************@g14g2000cwa.googlegroups .com>,
da*********@gmail.com wrote:
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 does it not work? I want to get rid of all ' ' and
':' in the string. I've checked the doc and from what I can tell this
is what strip() is supposed to do.


In /my/ docs, s.strip return a copy of s where all /leading/ and
/heading/ spaces are removed. s.strip(x) does the same but removing
chars of x.

So, what you're asking for by s.strip(' :') is "remove heading or
leading space or ':' chars", /not/ "remove or leading or
':' chars".

If you want to get rid of ' ' and ':' anywhere in s, i think that
string.maketrans and s.translate will do the job:
import string
s = 'p p:p'
ident = string.maketrans('', '')
s.translate(ident,' :')

'ppp'

--
Jaco
Nov 22 '05 #5
In article <11**********************@g14g2000cwa.googlegroups .com>,
da*********@gmail.com wrote:
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 does it not work? I want to get rid of all ' ' and
':' in the string. I've checked the doc and from what I can tell this
is what strip() is supposed to do.


In /my/ docs, s.strip return a copy of s where all /leading/ and
/heading/ spaces are removed. s.strip(x) does the same but removing
chars of x.

So, what you're asking for by s.strip(' :') is "remove heading or
leading space or ':' chars", /not/ "remove space or
':' chars".

If you want to get rid of ' ' and ':' anywhere in s, i think that
string.maketrans and s.translate will do the job:
import string
s = 'p p:p'
ident = string.maketrans('', '')
s.translate(ident,' :')

'ppp'

--
Jaco
Nov 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: qwweeeit | last post by:
Hi all, I need to limit as much as possible the lenght of a source line, stripping white spaces (except indentation). For example: .. . max_move and AC_RowStack.acceptsCards ( self,...
22
by: ineedyourluvin1 | last post by:
Hello all! I've been looking for a way to strip characters from strings such as a comma. This would be great for using a comma as a delimiter. I show you what I have right now. ...
16
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....
3
by: Michal A. Valasek | last post by:
Hello, I want to transform text with HTML markup to plain text. Is there some simple way how to do it? I can surely write my own function, which would simply strip everything with < and >....
6
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......
7
by: Nick | last post by:
strip() isn't working as i expect, am i doing something wrong - Sample data in file in.txt: 'AF':'AFG':'004':'AFGHANISTAN':'Afghanistan' 'AL':'ALB':'008':'ALBANIA':'Albania'...
4
by: Ethan Furman | last post by:
Greetings. The strip() method of strings works from both ends towards the middle. Is there a simple, built-in way to remove several characters from a string no matter their location? (besides...
0
by: Calvin Spealman | last post by:
On Jun 16, 2008, at 12:58 PM, Ethan Furman wrote: '.exaple.'
6
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.