1

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!

Tryph
  • 5,946
  • 28
  • 49
Pierfrancesco
  • 87
  • 1
  • 1
  • 10
  • 2
    Replace your `&` with an `and` – TomMP Apr 08 '19 at 14:01
  • Use the `and` keyword. – Tim Klein Apr 08 '19 at 14:01
  • As side note: you do not have to specify starting point for `range` if it is equal to 0, as it is default starting point. As you might check `print(range(0,12))` will give exactly same result as `print(range(12))` – Daweo Apr 08 '19 at 14:06
  • note that that the `"a "` string could not give the result you expect. start your range from 1, not 0 – Tryph Apr 08 '19 at 14:06

1 Answers1

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

DirtyBit
  • 16,613
  • 4
  • 34
  • 55
rdas
  • 20,604
  • 6
  • 33
  • 46