469,353 Members | 2,041 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

Re: 'string'.strip(chars)-like function that removes from the middle?



Cédric Lucantis wrote:
I don't see any string method to do that
>>'abcde'.translate(str.maketrans('','','bcd'))
'ae'

I do not claim this to be better than all the other methods,
but this pair can also translate while deleting, which others cannot.

Jun 27 '08 #1
4 2247
Terry Reedy wrote:
CĆ©dric Lucantis wrote:
>I don't see any string method to do that
>>'abcde'.translate(str.maketrans('','','bcd'))
'ae'

I do not claim this to be better than all the other methods,
but this pair can also translate while deleting, which others cannot.
You should mention that you are using Python 3.0 ;)
The 2.5 equivalent would be
>>u"abcde".translate(dict.fromkeys(map(ord, u"bcd")))
u'ae'

Peter
Jun 27 '08 #2
In article <g3*************@news.t-online.com>,
Peter Otten <__*******@web.dewrote:
>Terry Reedy wrote:
> >>'abcde'.translate(str.maketrans('','','bcd'))
'ae'
You should mention that you are using Python 3.0 ;)
The 2.5 equivalent would be
>>>u"abcde".translate(dict.fromkeys(map(ord, u"bcd")))
u'ae'
Only if you're using Unicode:
>>'abcde'.translate(string.maketrans('',''), 'bcd')
'ae'
>>sys.version_info
(2, 4, 4, 'final', 0)

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Jun 27 '08 #3
Sion Arrowsmith wrote:
In article <g3*************@news.t-online.com>,
Peter Otten <__*******@web.dewrote:
>>Terry Reedy wrote:
>> >>'abcde'.translate(str.maketrans('','','bcd'))
'ae'
You should mention that you are using Python 3.0 ;)
The 2.5 equivalent would be
>>>>u"abcde".translate(dict.fromkeys(map(ord, u"bcd")))
u'ae'

Only if you're using Unicode:
.... which is what you do if you are using the str type in 3.0.

Peter
Jun 27 '08 #4


Peter Otten wrote:
Terry Reedy wrote:
>CĆ©dric Lucantis wrote:
>>I don't see any string method to do that
'abcde'.translate(str.maketrans('','','bcd'))
'ae'

I do not claim this to be better than all the other methods,
but this pair can also translate while deleting, which others cannot.

You should mention that you are using Python 3.0 ;)
The 2.5 equivalent would be
>>>u"abcde".translate(dict.fromkeys(map(ord, u"bcd")))
u'ae'
Sorry. I did not realize that the maketrans function had not then been
moved from the string module to the str class object, unlike nearly all
other functions from string, including the translate function. I also
misremembered that maketrans did not have the new third delete param.
What I did do once long ago was something like
>>import string
a = 'abcde'.translate(string.maketrans('bd','**'))
a.replace('*','')
'ace'

which is not very nice.

tjr

Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Marcio Kleemann | last post: by
reply views Thread by William Stacey [MVP] | last post: by
8 posts views Thread by Nikolay Petrov | last post: by
7 posts views Thread by orangeDinosaur | last post: by
10 posts views Thread by Niklas Engfelt | last post: by
9 posts views Thread by Mosquito Man | last post: by
2 posts views Thread by tcomer | last post: by
1 post views Thread by O.B. | last post: by
3 posts views Thread by Colin J. Williams | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.