473,473 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2467
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
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...
1
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...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.