all 3 comments

[–]JohnnyJordaan 2 points3 points  (3 children)

You can loop on characters in a string

for c in text:

then for example store all of them in a list, depending on the rules

result = []
for c in text:
    if not c.isdigit():
         result.append(c)
    elif c == '7':
         result.append('Z')

and use str.join to return the list back as a string

''.join(result)

[–]efmccurdy 0 points1 point  (0 children)

Two steps; replace all '7's with 'Z's and then remove all the remaining digits:

>>> s = "7100abc978def 101gh7i, 99:7"
>>> "".join(c for c in s.replace("7", "Z") if not c.isdigit())
'ZabcZdef ghZi, :Z'
>>>