Split string of expression
<p>I want to split string like</p>

<pre><code>Expression = "((((324+17)*3)/((936-51)+124))-((13*(72-41))+6))"
</code></pre>

<p>I use str.split() but it split the number like: "3 2 4 + 1 7"</p>

<p>output:</p>

<pre><code>"( ( ( ( 324 + 17 ) * 3 ) / ( ( 936 - 51 ) + 124 ) ) - ( ( 13 * ( 72 - 41 ) ) + 6 ) )"
</code></pre>

<p>You're looking to <a href="https://docs.python.org/2/library/tokenize.html" rel="nofollow">tokenize</a> the string. For a Python expression you could do it using the tokenize module, or for a plainer form you could use various search functions. Here are two examples:</p>

<pre><code>&gt;&gt;&gt; expression = "((((324+17)*3)/((936-51)+124))-((13*(72-41))+6))"

&gt;&gt;&gt; import re
&gt;&gt;&gt; re.findall('[0-9]+|.', expression)
['(', '(', '(', '(', '324', '+', '17', ')', '*', '3', ')', '/', '(', '(', '936', '-', '51', ')', '+', '124', ')', ')', '-', '(', '(', '13', '*', '(', '72', '-', '41', ')', ')', '+', '6', ')', ')']

&gt;&gt;&gt; import tokenize
&gt;&gt;&gt; [t.string for t in tokenize.tokenize(iter([expression.encode('utf-8')]).__next__)
...  if t.type not in (tokenize.ENCODING, tokenize.ENDMARKER)]
['(', '(', '(', '(', '324', '+', '17', ')', '*', '3', ')', '/', '(', '(', '936', '-', '51', ')', '+', '124', ')', ')', '-', '(', '(', '13', '*', '(', '72', '-', '41', ')', ')', '+', '6', ')', ')']
</code></pre>

<p>The <code>iter([bytes]).__next__</code> part is required because tokenize normally reads out of files, and has no shortcut for a single string. </p>

python