How to replace characters in a string?

Chris Angelico rosuav at gmail.com
Wed Jun 8 05:17:28 EDT 2022


On Wed, 8 Jun 2022 at 19:13, Dave <dave at looktowindward.com> wrote:
>
> Hi,
>
> Thanks for this!
>
> So, is there a copy function/method that returns a MutableString like in objective-C? I’ve solved this problems before in a number of languages like Objective-C and AppleScript.
>
> Basically there is a set of common characters that need “normalizing” and I have a method that replaces them in a string, so:
>
> myString = [myString normalizeCharacters];
>
> Would return a new string with all the “common” replacements applied.
>
> Since the following gives an error :
>
> myString = 'Hello'
> myNewstring = myString.replace(myString,'e','a’)
>
> TypeError: 'str' object cannot be interpreted as an integer
>
> I can’t see of a way to do this in Python?
>

Not sure why you're passing the string as an argument as well as using
it as the object you're calling a method on. All you should need to do
is:

myString.replace('e', 'a')

ChrisA


More information about the Python-list mailing list