Problems with string and lists (searching and replaceing)

Bengt Richter bokr at oz.net
Sat Sep 20 17:30:37 EDT 2003


On Sat, 20 Sep 2003 21:08:40 +0200, "jblazi" <jblazi at hotmail.com> wrote:

>"Anton Vredegoor" <anton at vredegoor.doge.nl> schrieb im Newsbeitrag
>news:bkhnpn$hgc$1 at news.hccnet.nl...
>> >Do I oversee something?
>>
>> Yes, UserString. The documentation is a bit sparse, but reading the
>> module itself provides additional information. Below is a quick test
>> script.
>>
>> from UserString import MutableString
>>
>> def test():
>>     s = MutableString("helo world")
>>     print s
>>     x = 'helo'
>>     y = 'hello'
>>     i = s.find(x)
>>     s[i:i+len(x)] = y
>>     print s
>>
>> if __name__=='__main__':
>>     test()
>>
>> #output:
>> #helo world
>> #hello world
>
>Thx.
>It would be quite diffcult to explain this akward procedure to beginners.
>especially when they see that this can be done so simply in C.
>It seems that I have stumbled upon one of the few cases when Python is
>cumbersome and difficult to use.
>

For the above, what is wrong with

 >>> def test():
 ...     s = 'helo world'
 ...     print s
 ...     s = s.replace('helo','hello',1)
 ...     print s
 ...
 >>> test()
 helo world
 hello world

?

Regards,
Bengt Richter




More information about the Python-list mailing list