Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 30th, 2012  |  syntax: None  |  size: 0.41 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can I remove the single quotes from a string and use it for division in Python?
  2. a='/'
  3. b=6
  4. c=3
  5.  
  6. bac
  7.        
  8. a = "/"
  9. b = "6"
  10. c = "3"
  11. print eval(b + a + c)
  12.        
  13. import operator
  14.  
  15. a = operator.div
  16. b = 6
  17. c = 3
  18.  
  19. print a(b, c)
  20.        
  21. ops = {
  22.     "/": operator.div,
  23.     "*": operator.mul,
  24.     # et cetera
  25. }
  26.  
  27. a = ops["/"]
  28.        
  29. >>> a='/'
  30. >>> b=6
  31. >>> c=3
  32. >>> bac = ''.join(str(x) for x in (b, a, c))
  33. >>> bac
  34. '6/3'
  35.        
  36. >>> eval(bac)
  37. 2