I am trying to find an easy way to replace integers in a string with x in python. I was able to get something good by doing the following:
In [73]: string = "martian2015"
In [74]: string = list(string)
In [75]: for n, i in enumerate(string):
....: try:
....: if isinstance(int(i), int):
....: string[n]='x'
....: except ValueError:
....: continue
This actually gives something like the following:
In [81]: string
Out[81]: ['m', 'a', 'r', 't', 'i', 'a', 'n', 'x', 'x', 'x', 'x']
In [86]: joiner = ""
In [87]: string = joiner.join(string)
In [88]: string
Out[88]: 'martianxxxx'
My question is: is there a way to get the result in a simpler one without relying on error / exception handling?
Abdou source
share