1

I've been using in keyword to check if a character is available in a tuple and ran into a weird result. I'd appreciate if someone could explain the last result below. Thank you in advance.

>>> '' in ('n','p','')
True
>>> '' in ('n','p')
False
>>> '' in ('n')
True

Edit

As explained in Anthony's comment, it seems I've mistaken ('n') - which is just a string in parenthesis - as a tuple. Using ('n',) worked as expected.

1 Answers1

1

One item tuple, remember the commma:

thistuple = ("n",)
print(type(thistuple))

output:

<class 'tuple'>

but:

#NOT a tuple
thistuple = ("n")
print(type(thistuple))

output:

<class 'str'>
Salio
  • 1,058
  • 10
  • 21