Welcome to Python Land › Forums › The Python Tutorial › I need to get a True or False if a variable contains a string match
- This topic has 3 replies, 2 voices, and was last updated 6 days, 23 hours ago by
lostbit.
- AuthorPosts
- April 1, 2021 at 10:18 pm #2313
lostbit
ParticipantHow 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”)
- April 1, 2021 at 10:25 pm #2314
Erik van Baaren
KeymasterSo 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
- April 3, 2021 at 6:30 pm #2319
lostbit
ParticipantI find it strange so many python sites I searched
and no site had anything other than print()
Thank You
it worked
- April 4, 2021 at 3:07 pm #2320
lostbit
ParticipantI 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”)
- AuthorPosts
- You must be logged in to reply to this topic.