MasterHolbytla MasterHolbytla - 1 year ago 106
Python Question

Python detect character surrounded by spaces

Anyone know how I can find the character in the center that is surrounded by spaces?

1 + 1

I'd like to be able to separate the
in the middle to use in a if/else statement.

Sorry if I'm not too clear, I'm a Python beginner.

Answer Source

I think you are looking for something like the split() method which will split on white space by default.

Suppose we have a string s

s = "1 + 1"
chunks = s.split()
print(chunks[1]) # Will print '+'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download