The string might be:
JAIDK392**8'^+%&7JDJ0204İŞÇéS29487
I would like to remove everything from it but only leave behind numbers.
A simple way to do this is with the regular expression library re
:
>>> import re
>>> yourString = "JAIDK392**8'^+%&7JDJ0204İŞÇéS29487"
>>> numberOnlyString = re.sub('[^0-9]', '', yourString)
>>> print numberOnlyString
'39287020429487'
There's a way to do it without using any library. You can use the built-in function ord to get the ASCII code of a character. Then you can parse every character in your string to check if it is a number (If it is its ASCII code should be between 47 and 58.
str = "JAIDK392**8'^+%&7JDJ0204İŞÇéS29487" output = [] for char in str: if 47 < ord(char) < 58: output.append(char) result=''.join(output) print result
Regular expressions are great, but another way to do it would be:
>>> import string
>>> digits_only = "".join(_ for _ in your_string if _ in string.digits)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.