Date Tags python

Python does not allow string to be modified via indexed assignment. For example, a = "hello"; a[1] = "x" will produce a TypeError: 'str' object does not support item assignment error. Grr!

The following one-liner stridxrep() can be used if the need ever arises:

a = "hello"

# Python doesn't allow the following:
# a[1] = "x"

# A 'string index replace' function.
stridxrep = lambda s, i, r: "".join([(s[x] if x != i else r) for x in range(len(s))])

print stridxrep(a, 1, "x")
# Result: hxllo

print stridxrep(a, 0, chr(42))
# Result: *ello

Hi, I am Jeff Rimko!
A computer engineer and software developer in the greater Pittsburgh, Pennsylvania area.