In[1]: table = {ord(str(num)): None for num in range(10)}
In[2]: table
Out[2]:
{48: None,
49: None,
50: None,
51: None,
52: None,
53: None,
54: None,
55: None,
56: None,
57: None}
In[3]: "001file_name.txt".translate(table)
Out[3]: 'file_name.txt'
To create this table you can use str.maketrans method too.
table = str.maketrans('', '', '0123456789') # the characters from the third argument will be mapped to None
You get the same table:
{48: None,
49: None,
50: None,
51: None,
52: None,
53: None,
54: None,
55: None,
56: None,
57: None}
print(str.maketrans.__doc__)
Output:
Return a translation table usable for str.translate().
If there is only one argument, it must be a dictionary mapping Unicode
ordinals (integers) or characters to Unicode ordinals, strings or None.
Character keys will be then converted to ordinals.
If there are two arguments, they must be strings of equal length, and
in the resulting dictionary, each character in x will be mapped to the
character at the same position in y. If there is a third argument, it
must be a string, whose characters will be mapped to None in the result.