Hi,
a lot of times I need to replace more than one char into a string, so I
have to do something like
value = "test"
chars = "e"
for c in chars:
value = value.replace(c, "")
A solution could be that "replace" accept a tuple/list of chars, like
that was add into the new 2.5 for startswith.
I don't know, but can be this feature included into a future python release?
Thanks,
Michele 5 3109 A solution could be that "replace" accept a tuple/list of chars, like that was add into the new 2.5 for startswith.
I don't know, but can be this feature included into a future python release?
I don't know, but I think it would be useful
as for now I use this import re chars = re.compile(r'[abc]') text = "aAbBcCdD" chars.sub("",text)
'ABCdD'
Thanks, Michele
Regards, Daniel
> a lot of times I need to replace more than one char into a string, so I have to do something like
value = "test" chars = "e" for c in chars: value = value.replace(c, "")
A solution could be that "replace" accept a tuple/list of chars, like that was add into the new 2.5 for startswith.
I don't know, but can be this feature included into a future python release?
Well, another way of doing it would be values = "this is a test" chars = "aeiou" "".join([c for c in values if c not in chars])
'ths s tst'
If your either your chars is a large set or you're performing
this repeatedly with the same set of chars, you might want the
speed of membership-testing that one would get from a true set:
charset = set(chars) # do this once for the set # do the following as many times as you like in loops, etc. "".join([c for c in values if c not in charset])
'ths s tst'
HTH,
-tkc
In article <1a********************@twister2.libero.it>,
Michele Petrazzo <mi**************@TOGLIunipex.it> wrote: Hi, a lot of times I need to replace more than one char into a string, so I have to do something like
value = "test" chars = "e" for c in chars: value = value.replace(c, "")
A solution could be that "replace" accept a tuple/list of chars, like that was add into the new 2.5 for startswith.
I don't know, but can be this feature included into a future python release?
Let's say you want to make every vowel uppercase:
import string
trans_table = string.maketrans('aeiou', 'AEIOU')
"I don't know, but can be this feature included into".translate(trans_table)
prints:
"I dOn't knOw, bUt cAn bE thIs fEAtUrE InclUdEd IntO"
That more than addresses your requirements, as it can do multiple
character substitutions multiple times in one call.
--
Jim Segrave (je*@jes-2.demon.nl)
Check out the .translate method and the string.maketrans documentation.
You can use it to delete a list of characters all in one line: s = "I am the walrus" import string s.translate(string.maketrans("",""),"aeiou")
'I m th wlrs'
Michele Petrazzo wrote: Hi, a lot of times I need to replace more than one char into a string, so I have to do something like
value = "test" chars = "e" for c in chars: value = value.replace(c, "")
A solution could be that "replace" accept a tuple/list of chars, like that was add into the new 2.5 for startswith.
I don't know, but can be this feature included into a future python release?
Thanks, Michele jo********@gmail.com wrote: Check out the .translate method and the string.maketrans documentation. You can use it to delete a list of characters all in one line:
Yes. This is, more or less, what I were looking for.
P.s. Sure, if replace could accept a tuple... :)
Thanks to all,
Michele This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: higabe |
last post by:
Three questions
1)
I have a string function that works perfectly but according to W3C.org
web site is syntactically flawed because it contains the characters </
in sequence. So how am I...
|
by: M |
last post by:
Hi,
I've searched through the previous posts and there seems to be a few
examples of search and replacing all occurrances of a string with
another string.
I would have thought that the code...
|
by: dimitris67 |
last post by:
How can I replace an occurence of p(a string) in an other string(s) with
np(new string)..
char* replace _pattern(char *s,char *p,char *np)
PLEASE HELP ME!!!!!
|
by: VMI |
last post by:
If I have the string "Héllo", how can I replace char (é) with an 'e'? I
cannot use the String.Replace() fuction. It has to be by replacing one char
with another.
Thanks.
|
by: tshad |
last post by:
Can you do a search for more that one string in another string?
Something like:
someString.IndexOf("something1","something2","something3",0)
or would you have to do something like:
if...
|
by: Jeff S |
last post by:
In a VB.NET code behind module, I build a string for a link that points to a
JavaScript function. The two lines of code below show what is relevant.
PopupLink = "javascript:PopUpWindow(" &...
|
by: Crirus |
last post by:
dim pp as string
pp="{X=356, Y=256}{X=356, Y=311.2285}{X=311.2285, Y=356}{X=256,
Y=356}{X=200.7715, Y=356}{X=156, Y=311.2285}{X=156, Y=256}{X=156,
Y=200.7715}{X=200.7715, Y=156}{X=256,...
|
by: Peter Row |
last post by:
Hi,
I know this has been asked before, but reading the threads it is still not
entirely clear.
Deciding which .Replace( ) to use when.
Typically if I create a string in a loop I always use a...
|
by: morleyc |
last post by:
Hi, i would like to remove a number of characters from my string (\t
\r \n which are throughout the string), i know regex can do this but i
have no idea how. Any pointers much appreciated.
Chris
|
by: kronus |
last post by:
I'm receiving an xml file that has a child called modified and it represents a date value in the form of a string -- Nov 14, 2008 -- and in my app, I have items associated with each object and I'm...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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,...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |