And operator to check equality between strings

时间:2019-04-08 14:00:34

标签: python operator-keyword

I'm trying to check if an element of a string has a blank space on the left and on the right. I'm trying to use an and operator in Python, but this error occurs:

unsupported operand type(s) for &: 'str' and 'str'

This is my code:

for i in range(0,len(s_sentence)-1):
    if (s_sentence[i-1]==' ' & s_sentence[i+1]==' '):
        [...]

How can I fix this? thank you!

1 个答案:

答案 0 :(得分:3)

if (s_sentence[i-1]==' ' and s_sentence[i+1]==' '):

& is not the operator you're looking for, and is.

Sidenote: & does bitwise AND in Python