
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Can we do math operation on Python Strings?
You can use the eval function to evaluate mathematical expressions in strings.
Example
For example, if you have a string with the content (4*5) + 21, you can eval it and get the result.
>>> s = "(4*5) + 22" >>> eval(s) 42
Eval follows Python rules for evaluating mathematical expressions for cases in which parenthesis are not provided, etc. Be very careful when using eval though as it can be a source of huge security loopholes and bugs.
- Related Articles
- How can we do date and time math in Python?
- How to do Python math at command line?
- How To Do Math With Lists in python ?
- How to force JavaScript to do math instead of putting two strings together?
- MongoDB aggregation / math operation to sum score of a specific student
- What can we do to reduce acid rain on the Earth?
- How can we do Python operator overloading with multiple operands?
- How can we concatenate two strings using jQuery?
- Math Operations on BigInteger in Java
- Program to count number of strings we can make using grammar rules in Python
- How can we do the basic print formatting for Python numbers?
- What does % do to strings in Python?
- Can we use Switch statement with Strings in java?
- Python – Split Strings on Prefix Occurrence
- How do we mix two strings and generate another in java?

Advertisements