-2

This is my code:

for post in socket("news", start=currentTime):
    if post["body"] == 'xyz' and post["author"] == 'admin' and post["url"] == 'latestnews':
        try: 
 print ("Found it!")
(...)

It's working, but only if post["body"] is "xyz". If post["body"] is "Bla bla bla xyz" then it's not working. What should I do if I want my script to dispaly "Found it!" even if post["body"] contains

"Lorem ipsum dolor sit amet, eum ne maiorum volutpat, ei has erat eruditi, autem fierent evertitur at has xyz Case simul persius id mei, soleat laoreet assentior ea mel. Meis assum contentiones in cum, est ornatus salutandi id. Sanctus labores ius ne."

Sorry, but I'm new to Python :-/

Psidom
  • 209,562
  • 33
  • 339
  • 356

2 Answers2

0

All you have to do is change your ordering. You can use the operator 'in' to check if a string contains another string.

if 'xyz' in 'oiurwernjfndj xyz iekjerk':
    print True
Polosky
  • 88
  • 5
  • 13
-2

You can probably just use in operator or str.find: https://docs.python.org/3/reference/expressions.html#in https://docs.python.org/2/library/stdtypes.html?highlight=index#str.find

Basically, you would do

if(xyz in post["body"] ...) { 
// OR 
if(post["body"].find('xyz') != -1 and ... ) {

The in operator is probably the fastest way.

The .find will return a number between 0 and string length -1 (to tell you the location of the string. However, if it cannot find the string, it returns a -1. So if it returns -1, then it isn't there, so anything other than -1 means it is.

Architect Nate
  • 674
  • 1
  • 6
  • 21