I need to get a True or False if a variable contains a string match

Welcome to Python Land Forums The Python Tutorial I need to get a True or False if a variable contains a string match

Viewing 3 reply threads
  • Author
    Posts
    • #2313
      lostbit
      Participant

      How can this be so hard, I have tried so many things.

      So I have to search or compare s string to the value in a variable.

      I just do not know how!

      This scripts prints D90 and Fount IT, I want  instead a way to get a 0 or 1

      so I can get trigger an LED if it is 0, a different LED if it is 1.

      import sys

      import os

      i =  (“D90”)

      print(i)

      if i == (“D90”):

      print(“Found IT”)

       

    • #2314
      Erik van Baaren
      Keymaster

      So you want something like this, I think:

      import sys
      import os
      
      i =  (“D90”)
      print(i)
      
      if i == (“D90”):
          # it's true, so set led
      else:
          # it's false, set other led

       

    • #2319
      lostbit
      Participant

      I find it strange so many python sites I searched

      and no site had anything other than print()

      Thank You

      it worked

    • #2320
      lostbit
      Participant

      I applied the if command to the real script and it does not work.

      When check_camera has a groping of words including D90 it is not found.

      I tried split I tried find with errors find not defined or missing attribute!!

      I created a small script with check_camera = to the full string and again

      check_camera = to just D90.

      Here is the output

      import time
      import os
      import sh
      import sys

      # below I just populate check_camera with the string coppied from
      # shell terminal on my rasberry PI. The output print check_camera
      # prints the same string. but if does not work.

      print(“checking_camera”)
      check_camera = (“Nikon DSC D90 (PTP mode) usb:001,005”)
      print(check_camera)
      if check_camera == (“D90”):
      print(“found_camera”)
      else:
      print(“oops”)

      #now check_camera only contains D90
      print(“checking_camera”)
      check_camera = (“D90”)
      print(check_camera)
      if check_camera == (“D90”):
      print(“OK”)
      else:
      print(“shucks”)

      import time
      import os
      import sh
      import sys

      # below I just populate check_camera with the string coppied from
      # shell terminal on my rasberry PI. The output print check_camera
      # prints the same string. but if does not work.

      print(“checking_camera”)
      check_camera = (“Nikon DSC D90 (PTP mode) usb:001,005”)
      print(check_camera)
      if check_camera == (“D90”):
      print(“found_camera”)
      else:
      print(“oops”)

      #now check_camera only contains D90
      print(“checking_camera”)
      check_camera = (“D90”)
      print(check_camera)
      if check_camera == (“D90”):
      print(“OK”)
      else:
      print(“shucks”)

       

      import time
      import os
      import sh
      import sys

      # below I just populate check_camera with the string coppied from
      # shell terminal on my rasberry PI. The output print check_camera
      # prints the same string. but if does not work.

      print(“checking_camera”)
      check_camera = (“Nikon DSC D90 (PTP mode) usb:001,005”)
      print(check_camera)
      if check_camera == (“D90”):
      print(“found_camera”)
      else:
      print(“oops”)

      #now check_camera only contains D90
      print(“checking_camera”)
      check_camera = (“D90”)
      print(check_camera)
      if check_camera == (“D90”):
      print(“OK”)
      else:
      print(“shucks”)

Viewing 3 reply threads
  • You must be logged in to reply to this topic.