Check if space is in a string

前端 未结 5 2257
执笔经年
执笔经年 2021-02-11 14:08
\' \' in word == True

I\'m writing a program that checks whether the string is a single word. Why doesn\'t this work and is there any better way to che

5条回答
  •  执念已碎
    2021-02-11 14:37

    Write if " " in word: instead of if " " in word == True:.

    Explanation:

    • In Python, for example a < b < c is equivalent to (a < b) and (b < c).
    • The same holds for any chain of comparison operators, which include in!
    • Therefore ' ' in w == True is equivalent to (' ' in w) and (w == True) which is not what you want.

提交回复
热议问题