How to replace characters in a string?

Dennis Lee Bieber wlfraed at ix.netcom.com
Wed Jun 8 13:55:10 EDT 2022


On Wed, 8 Jun 2022 11:09:05 +0200, Dave <dave at looktowindward.com> declaimed
the following:

>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.

	There are no mutable strings in Python. Any operation manipulating a
string RETURNS A MODIFIED NEW STRING.

>myString = 'Hello'
>myNewstring = myString.replace(myString,'e','a’)
>

	Please study the library reference manual -- it should be clear what
the various string methods can perform. Hint: they are "methods", which
means whatever is before the . becomes the automatic "self" argument inside
the method)

https://docs.python.org/3/library/stdtypes.html#string-methods

"""
str.replace(old, new[, count])

    Return a copy of the string with all occurrences of substring old
replaced by new. If the optional argument count is given, only the first
count occurrences are replaced.
"""

	myNewstring = myString.replace("e", "a")

	However... Please study
"""
static str.maketrans(x[, y[, z]])

    This static method returns a translation table usable for
str.translate().

    If there is only one argument, it must be a dictionary mapping Unicode
ordinals (integers) or characters (strings of length 1) to Unicode
ordinals, strings (of arbitrary lengths) or None. Character keys will then
be converted to ordinals.

    If there are two arguments, they must be strings of equal length, and
in the resulting dictionary, each character in x will be mapped to the
character at the same position in y. If there is a third argument, it must
be a string, whose characters will be mapped to None in the result.
"""
"""
str.translate(table)

    Return a copy of the string in which each character has been mapped
through the given translation table. The table must be an object that
implements indexing via __getitem__(), typically a mapping or sequence.
When indexed by a Unicode ordinal (an integer), the table object can do any
of the following: return a Unicode ordinal or a string, to map the
character to one or more other characters; return None, to delete the
character from the return string; or raise a LookupError exception, to map
the character to itself.

    You can use str.maketrans() to create a translation map from
character-to-character mappings in different formats.

    See also the codecs module for a more flexible approach to custom
character mappings.
"""

	Hmmm, I'm out-of-date... I'm on v3.8 and .removeprefix() and
.removesuffix() (from v3.9) simplify my previous post... Instead of

	if myString.lower().endswith(".mp3"): #lower() is a precaution for case
		myString = myString[:-4]

just use
		myString = myString.lower().removesuffix(".mp3")
{note, you'll have to make the compare using .lower() on the other name
since this statement returns a lowercased version}


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/


More information about the Python-list mailing list