Python detect character surrounded by spaces
<p>Anyone know how I can find the character in the center that is surrounded by spaces?</p>

<p><code>1 + 1</code></p>

<p>I'd like to be able to separate the <code>+</code> in the middle to use in a if/else statement.</p>

<p>Sorry if I'm not too clear, I'm a Python beginner.</p>

<p>Not knowing how many spaces separate your central character, then I'd use the following:</p>

<pre><code>s = '1 +   1'
middle = filter(None, s.split())[1]
print middle  # +
</code></pre>

<p>The <code>split</code> works as in the solution provided by Zac, but if there are more than a single space, then the returned list will have a bunch of <code>''</code> elements, which we can get rid of with the <code>filter(None, )</code> function.</p>

<p>Then it's just a matter of extracting your second element.</p>

<p>Check it in action at <a href="https://eval.in/636622" rel="nofollow">https://eval.in/636622</a></p>

<p>If we look at it step-by-step, then here is how it all works using a python console:</p>

<pre><code>&gt;&gt;&gt; s = '1 +   1'

&gt;&gt;&gt; s.split()
['1', '+', '', '', '1']

&gt;&gt;&gt; filter(None, s.split())
['1', '+', '1']

&gt;&gt;&gt; filter(None, s.split())[1]
'+'
</code></pre>

python python-3.x