Advertisement
user_137

string to int (+ regex version)

Feb 15th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. oldstring = '134uu8'
  2. print 'the initial string is: ', oldstring, '\n'
  3. # int(oldstring)
  4. # ValueError: invalid literal for int() with base 10: '134uu8'
  5.  
  6. newstring = [x for x in oldstring if x in "0123456789"]
  7. print 'after list comprehension', newstring
  8. newstring = "".join(newstring)
  9. print 'after joining the list strings together', newstring, '\n'
  10. now_an_int = int(newstring)
  11. print 'applying int function to the purified string', now_an_int
  12. print 'and its type is: ', type(now_an_int), '\n\n'
  13.  
  14.  
  15. # import re
  16. # now_an_int = int(re.sub('[^0-9]', '', oldstring))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement