Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > 'string'.strip(chars)-like function that removes from the middle?

Reply
Thread Tools

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

 
 
Ethan Furman
Guest
Posts: n/a
 
      06-16-2008
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 .replace()

For example:
..strip --> 'www.example.com'.strip('cmowz.')
'example'
..??? --> --- 'www.example.com'.strip('cmowz.')
'exaple'
--
Ethan

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      06-16-2008
Ethan Furman wrote:

> 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 .replace()


>>> identity = "".join(map(chr, range(256)))
>>> 'www.example.com'.translate(identity, 'cmowz.')

'exaple'

Peter
 
Reply With Quote
 
 
 
 
Raymond Hettinger
Guest
Posts: n/a
 
      06-16-2008
On Jun 16, 10:09*am, Peter Otten <__pete...@web.de> wrote:
> Ethan Furman wrote:
> > 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 .replace()
> >>> identity = "".join(map(chr, range(256)))
> >>> 'www.example.com'.translate(identity, 'cmowz.')

>
> 'exaple'


And in Py2.6, you'll be able to simplify further:

>>> 'abcde'.translate(None, 'bd')

'ace'


Raymond
 
Reply With Quote
 
Roel Schroeven
Guest
Posts: n/a
 
      06-17-2008
Peter Otten schreef:
> Ethan Furman wrote:
>
>> 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 .replace()

>
>>>> identity = "".join(map(chr, range(256)))


Or

identity = string.maketrans('', '')

>>>> 'www.example.com'.translate(identity, 'cmowz.')

> 'exaple'



--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov

Roel Schroeven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Re: 'string'.strip(chars)-like function that removes from the middle? Terry Reedy Python 4 06-17-2008 05:51 PM
[OT] Microsoft's AntiSpyware Tool removes Internet Explorer T-Bone MCSE 11 01-19-2005 08:18 PM
VS.NET removes "Runat=Server" without asking??? Ronald Colijn ASP .Net 1 11-27-2003 09:01 AM
function that removes the punctuation and some characters like (*&^%$#@!<>?"} from a text string Beznas ASP General 8 09-10-2003 05:34 PM



Advertisments