I have strings like '12454v', '346346z'. I want to delete all letters from strings.
Re works fine:
import re
str='12454v'
re.sub('[^0-9]','', str)
#return '12454'
Is there a way to do this without using regular expressions?
In Python you can use the replace() and translate() methods to specify which characters you want to remove from the string and return a new modified string result. It is important to remember that the original string will not be altered because strings are immutable.
Using nested replace() In the program given below, we will see how replace() is used to remove multiple characters from the string. In the first step, we have initialized the string whose characters we would like to replace.
We can remove part of the string using REPLACE() function. We can use this function if we know the exact character of the string to remove. REMOVE(): This function replaces all occurrences of a substring within a new substring.
>>> ''.join(filter(str.isdigit, '12454v'))
'12454'
In python 2 the second argument to the translate
method allows you to specify characters to delete
http://docs.python.org/2/library/stdtypes.html#str.translate
The example given shows that you can use None
as a translation table to just delete characters:
>>> 'read this short text'.translate(None, 'aeiou')
'rd ths shrt txt'
(You can get a list of all ASCII letters from the string module as string.letters
.)
Update: Python 3 also has a translate
method, though it requires a slightly different setup:
from string import ascii_letters
tr_table = str.maketrans({c:None for c in ascii_letters})
'12345v'.transate(tr_table)
For the record, using translation tables in Python 2 is much, much faster than the join/filter method:
>>> timeit("''.join(filter(lambda c:not c.isalpha(), '12454v'))")
2.698641061782837
>>> timeit("''.join(filter(str.isdigit, '12454v'))")
1.9351119995117188
>>> timeit("'12454v'.translate(None, string.letters)", "import string")
0.38182711601257324
Likewise in Python 3:
>>> timeit("'12454v'.translate(tr_table)", "import string; tr_table=str.maketrans({c:None for c in string.ascii_letters})")
0.6507143080000333
>>> timeit("''.join(filter(lambda c:not c.isalpha(), '12454v'))")
2.436105844999929
I think you can try this with .translate
method.
>>> import string
>>> str='12454v'
>>> str.translate(None, string.letters)
'12454'
There is a very good answer about .translate
method here.
This is a somewhat less elegant than the others because it's not using a specific function and is somewhat more clunky:
newStr = ''
myStr='12454v'
for char in myStr:
try:
newStr += str(int(char))
except ValueError:
pass
print newStr
Again, this isn't best way, but I'm just throwing it out there.
I converted it to an int
first so that it can check whether or not is an integer. Then, I convert it to a str
so that it can be added to newStr
.
On another note, you shouldn't use str
as a variable name because it shadows the built-in function str()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With