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

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

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

Jun 27 '08 #1
4 4180
Ethan Furman wrote:
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
filter()
>>removeChars = ';j'
filter(lambda c: c not in removeChars, x)
'asdfklasdfkl'
>>>
or

a list comprehension

x="asdfjkl;asdfjkl;"
>>''.join([c for c in x if c not in ';'])
'asdfjklasdfjkl'

-Larry
Jun 27 '08 #2
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
Jun 27 '08 #3
On Jun 16, 10:09*am, Peter Otten <__pete...@web.dewrote:
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
Jun 27 '08 #4
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
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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 = ' ...
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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...

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.