|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Calculate String
Help, I have a string that contains a mathematical expression(very simple can only contain +,-,*,and\) How do I get python to evaluate that string? Thanks, btw I need to ensure that the divison is integer division. Thanks!
example str="3+5*2\1" Last edited by jimo9 : March 30th, 2004 at 10:30 PM. |
|
#2
|
||||
|
||||
|
The eval() function is what you want:
Code:
>>> eval("1+1/2")
1
>>> eval("1+1/2.0")
1.5
>>> eval("1+1//2")
1
>>> eval("1+1//2.0")
1.0
>>> string = "1+1/2.0".replace('/','//')
>>> string
'1+1//2.0'
Grim ![]()
__________________
*** Experimental Python Markup CGI V2 *** |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Calculate String |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|