#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    10
    Rep Power
    0

    quick question about "in"


    when you say for example

    for letter in txt:

    if this in that

    "in" signifies membership... how can i saw if something is NOT in something else

    if this (is not) in that:
    do whatever
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Location
    Orlando FL
    Posts
    130
    Rep Power
    13
    just use "not in". For example:

    Code:
    if 1 not in [1,2,4]: print "IN"
    
    or 
    
    if 1 in [1,2,4]: print "IN"
    both case will print out "IN".
    Cheers.
    Help me, help you, help others...
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    10
    Rep Power
    0
    thanks a lot
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373

    using "in" with strings


    incidentally, from Python 2.3 "in" (and "not in") can be used to test for substrings within a string, not just for single characters. For example:

    Code:
    >>> 'hell' in 'hello world'
    True
    >>> 'heaven' not in 'hello world'
    True
    >>>
    Regards,

    Dave - The Developers' Coach
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    33
    Rep Power
    10
    i got a similiar question.

    can i use two variables(strings) using 'in'.
    i want to test if one variable is in another, so i did this:

    if letter in word:
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Originally Posted by megazear7
    i got a similiar question.

    can i use two variables(strings) using 'in'.
    i want to test if one variable is in another, so i did this:

    if letter in word:
    The quickest way to answer any question like this is to try it and see. Python has a very good interactive mode, which lets you execute snippets of code and see the result immediately. It also has a very good online help mode.

    I find it is often much quicker to experiment in the interactive console than to look up something in docs, let alone post a question to a forum and wait for someone to reply.
    Dave

    P.S. the answer to your question is YES.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    33
    Rep Power
    10
    ok thanks for the tip
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    33
    Rep Power
    10
    it says: "reguires character as left operand" when i do it.
    for :

    if guess in word:

    and i cant change the two variables around becouse 'word' is a random word from a list and 'guess' is a letter the user guesses.??

IMN logo majestic logo threadwatch logo seochat tools logo