python字元串notin
發布時間: 2025-08-14 13:46:58
A. Python中關系運算符in,not in在字元串表達式和列表的使用時有什麼區別和注意點
Membership test operations
For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expressionxinyis equivalent toany(xiseorx==eforeiny).
For the string and bytes types,xinyisTrueif and only ifxis a substring ofy. An equivalent test isy.find(x)!=-1. Empty strings are always considered to be a substring of any other string, so""in"abc"will returnTrue.
翻譯:
對容器類型,例如list、tuple、set、frozenset、dict或collections.deque,表達式x in y等價於any(x is e or x == e for e in y)。
對字元串和bytes類型,x in y為真當且僅當x是y的子串。等價測試為y.find(x) != -1。空字元串永遠被視作是其他任何字元串的子集,因此"" in "abc"將返回True。
熱點內容