和运算符来检查字符串之间的相等性[重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

我正在尝试检查字符串元素的左侧和右侧是否有空格。我正在尝试在Python中使用和运算符,但是会发生以下错误:

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

这是我的代码:

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

我怎样才能解决这个问题?谢谢!

python operator-keyword
1个回答
3
投票
if (s_sentence[i-1]==' ' and s_sentence[i+1]==' '):

&不是你想要的运营商,and是。

旁注:&在Python中使用按位AND

© www.soinside.com 2019 - 2024. All rights reserved.