By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,386 Members | 1,156 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,386 IT Pros & Developers. It's quick & easy.

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

P: n/a


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
Share this Question
Share on Google+
4 Replies

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a


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.