Reputation: 19329
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
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