Re: Replace single character at given position

2006-09-27 Thread Magnus Lycka
Larry Bates wrote: >> How do I replace a single character in a string at a given position? You can't. Strings can't be mutated in Python how ever hard you try. The string type is immutable. (Of course, I suspect you can write a horrible C extension that would help you cheat. Yuk!) You need to cre

Re: Replace single character at given position

2006-09-20 Thread Roberto Bonvallet
[EMAIL PROTECTED] wrote: > Martin Kulas: >> Are there better (faster) ways to achieve my goal? >> I have looked through the methods of type ``string'' >> but I have not found any appropriate method or function. [...] > A second way is to use a list of strings with len=1. Like this: idx = 1 >>>

Re: Replace single character at given position

2006-09-19 Thread Larry Bates
Martin Kulas wrote: > Hello! > > How do I replace a single character in a string at a given position? > > From programming languages like C I expect something like that: > idx = 1 s1 = "pxthon" s1[idx] = 'y' > Traceback (most recent call last): > File "", line 1, in ? > TypeErro

Re: Replace single character at given position

2006-09-19 Thread bearophileHUGS
Martin Kulas: > Are there better (faster) ways to achieve my goal? > I have looked through the methods of type ``string'' > but I have not found any appropriate method or function. Python strings are immutable, so you can't modify them, so you can't find methods to change them. I agree that someti

Re: Replace single character at given position

2006-09-19 Thread Bjoern Schliessmann
Martin Kulas wrote: > From programming languages like C I expect something like that: > idx = 1 s1 = "pxthon" s1[idx] = 'y' > Traceback (most recent call last): > File "", line 1, in ? > TypeError: object does not support item assignment > > It does not work :-( Yes, Python str

Replace single character at given position

2006-09-19 Thread Martin Kulas
Hello! How do I replace a single character in a string at a given position? >From programming languages like C I expect something like that: >>> idx = 1 >>> s1 = "pxthon" >>> s1[idx] = 'y' Traceback (most recent call last): File "", line 1, in ? TypeError: object does not support item assignme