Python string in ... in syntax

It is strange to see a piece of code using the syntax 'str in str in str', for example:

>>> 'test' in 'testtest' in 'testtesttest' True >>> 'test' in 'testtest' in 'tb3' False >>> 'test' in 'testtesta' in 'testtesttest' False >>> 'test' in ('testtest' in 'testtesttest') Traceback (most recent call last): File "<input>", line 1, in <module> 'test' in ('testtest' in 'testtesttest') TypeError: argument of type 'bool' is not iterable 

It seems that "in ... in ..." is like a comparison of "<... <...". But fast Google did not lead me to official answers. Any help?

+5
source share
1 answer

The official answer is the Python documentation :

 comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "!=" | "is" ["not"] | ["not"] "in" 

The in keyword is a comparison operator. And "Comparison can be copied arbitrarily." Note that this is not limited to “value matching” ( > , == , etc.).

This code checks to see if each substring of the next element in the chain.

+5
source

Source: https://habr.com/ru/post/1266669/


All Articles