473,320 Members | 1,947 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,320 software developers and data experts.

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 2452
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Marcio Kleemann | last post by:
I need to force the first letter of each word in a line of text to uppercase. The text comes from a TextBox control in a Web Form. I'm new to ..NET and am having a problem. Since I can't modify...
0
by: William Stacey [MVP] | last post by:
Had a method that got some string info from mp3 tags in N files and serializes this class and deserializes at other side. Works ok except sometimes get chars that choke the XmlSerializer. After...
8
by: Nikolay Petrov | last post by:
I need a way to strip chars from a string. The chars are all chars that are not allowed in file path. TIA
7
by: orangeDinosaur | last post by:
Hello, I am encountering a behavior I can think of reason for. Sometimes, when I use the .strip module for strings, it takes away more than what I've specified. For example: >>> a = ' ...
10
by: Niklas Engfelt | last post by:
Does anyone have a nice algorithm/method to increase all digits one step, e.g. 123123 to 234234 and 4444 to 5555 and 999999 to 000000? Best regards Niklas
33
by: sklett | last post by:
I need to take a string, make the first character lowercase and prepend an underscore character to it. Something like this: "California" "_california" Here is the (ugly) solution I cam up...
9
by: Mosquito Man | last post by:
string s("0101"); I can't change it into s=1101 by s='1'; because string.this is read-only, no assignment is allowed :-( thank you for your help
2
by: tcomer | last post by:
Hello. I'm working on a client/server application and I'm having some weird issues with concatenating strings. The client program sends some text: Object objData = "IssuedCommand:SenderName";...
1
by: O.B. | last post by:
In the example below, I'm trying to convert a fixed byte array to a string. I get an error about needing to use "fixed" but I have no clue where to apply it. Help? using System; using...
3
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....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.