string.replace doesn't removes ":"

Johannes Bauer dfnsonfsduifb at gmx.de
Sun Feb 10 11:36:53 CET 2013


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>



More information about the Python-list mailing list