Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
edited tags
Link
Moinuddin Quadri
  • 48.4k
  • 13
  • 101
  • 138
edited tags
Link
Moinuddin Quadri
  • 48.4k
  • 13
  • 101
  • 138
Source Link
Yuta73
  • 198
  • 10

Python newbie clarification about tuples and strings

I just learned that I can check if a substring is inside a string using:

substring in string

It looks to me that a string is just a special kind of tuple where its elements are chars. So I wonder if there's a straightforward way to search a slice of a tuple inside a tuple. The elements in the tuple can be of any type.

tupleslice in tuple

Now my related second question:

>>> tu = 12 ,23, 34,56
>>> tu[:2] in tu
False

I gather that I get False because (12, 23) is not an element of tu. But then, why substring in string works?. Is there syntactic sugar hidden behind scenes?.