string.replace doesn't removes ":"

vduncan80 at gmail.com vduncan80 at gmail.com
Tue Feb 12 10:14:48 EST 2013


On Sunday, February 10, 2013 4:36:53 AM UTC-6, Johannes Bauer wrote:
> On 09.02.2013 12:04, Joshua Robinson wrote:
> 
> > Hi *Monte-Pythons*,
> 
> > 
> 
> > x = "this is a simple : text: that has colon"
> 
> > s = x.replace(string.punctuation, "");  OR
> 
> > s = x.replace(string.punctuation, "");
> 
> > print x   # 'this is a simple : text: that has colon'
> 
> > # The colon is still in the text !!!!
> 
> > 
> 
> > Is this a bug or am I doing something wrong ?
> 
> 
> 
> The latter. str.replace() only replaces complete substrings, not single
> 
> character occurences of the given pattern. That is
> 
> 
> 
> "foo".replace("foo", "bar") == "bar"
> 
> "foofoo".replace("foo", "bar") == "barbar"
> 
> "foofoo".replace("fo", "bar") == "barobaro"
> 
> "foofoo".replace("abcdef", "bar") == "foofoo"
> 
> 
> 
> Regards,
> 
> Johannes
> 
> 
> 
> -- 
> 
> >> Wo hattest Du das Beben nochmal GENAU vorhergesagt?
> 
> > Zumindest nicht öffentlich!
> 
> Ah, der neueste und bis heute genialste Streich unsere großen
> 
> Kosmologen: Die Geheim-Vorhersage.
> 
>  - Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$1 at speranza.aioe.org>

Hello Joshua:

Hopefully you have worked out the issue.  Johannes is right on the money using 'replace' as shown below.

x = "this is a simple : text: that has colon
s = x.replace(":", "")
print(s)
'this is a simple  text that has colon'

Sincerely,
VDuncan



More information about the Python-list mailing list