alphanumeric
alphanumeric

Reputation: 19329

How to replace numeric characters

In every name in names:

names = ['name_9', 'name_xyz', 'name_10', 'name_11']

I want to replace the numbers with a single '*' star character so the final result would look like:

result = ['name_*', 'name_xyz', 'name_*', 'name_*']

(Please note that some of the names in names could contain no digits (such as 'name_xyz'. And a single star * character replaces any number regardless on how many digits the number contains... so a number 4 is replaces with a single * star and the number 444 is also replaced with a single * star).

How to get it done?

Upvotes: 0

Views: 43

Answers (1)

ma2moun
ma2moun

Reputation: 80

You need to import re as

import re

then add a for loop with a regex

for x in names:
     print(re.sub('\d+','*',x))

Upvotes: 1

Related Questions