how to check two conditions in a single block rather than 2 in python

Viewed 42
            if 'hiring ' in job_title:
                job_title = job_title.split("hiring ")[1]
                if job_title.startswith('– '):
                    job_title = job_title.replace('– ', '')
            elif 'Hiring ' in job_title:
                job_title = job_title.split("Hiring ")[1]
                if job_title.startswith('– '):
                    job_title = job_title.replace('– ', '')
            job_titles.append(job_title)

So I have this snippet of code and I want to check if the string variable job_title has either 'hiring' or 'Hiring' in it and then get the text after this word by splitting job_title...I want to do it in a single if statement...but right now I have two...How can I do that because for a single statement:

            if 'hiring ' or 'Hiring ' in job_title:
                job_title = job_title.split("hiring ")[1]
                if job_title.startswith('– '):
                    job_title = job_title.replace('– ', '')

I have to split at either 'hiring' or 'Hiring'... Any ideas?

2 Answers

Here is a method using a regular expression to perform a case insensitive substitution on the part of the string up to and including the first instance of "hiring", optionally followed by a sequence of whitespace, optionally followed by a single dash, and optionally followed by a further whitespace sequence.

This is the pattern : r'(?i)^.*?hiring\s*-?\s*'

Here it is in action:

import re

word = 'hiring'
pattern = rf'(?i)^.*?{word}\s*-?\s*'

test_strings = ['Example Co. is Hiring example staff for example.',
                'Example Co. is hiring example staff for example.',
                'Example Co. is HiRiNg example staff for example.',
                'Example Co. is Hiring - example staff for example.',
                'Example Co. is Hiring     -   example staff for example.',
                'Example Co. is example staff for example.',
                'Example Co. is Hiring example staff for hiring example.',
               ]

for s in test_strings:
    print(re.sub(pattern, '', s))

Output

example staff for example.
example staff for example.
example staff for example.
example staff for example.
example staff for example.
Example Co. is example staff for example.
example staff for hiring example.

Consistent with your code, the last test string shows that the substitution is only up to the first instance of the word "hiring". If that word occurs later in the string it is not considered. If you would prefer the substitution to be applied up to the last instance you can remove the non-greedy modifier preceding the target word, i.e.

>>> pattern = rf'(?i)^.*{word}\s*-?\s*'
>>> print(re.sub(pattern, '', test_strings[-1]))
example.

You can use the following condition for your purposes. It makes use of Walrus operator
See examples for behavior. Use strip() if you do not want the space.

>>> def get_text(text):
...     if (len(info:=text.lower().split('hiring'))>1):
...             return info[-1]
...     else:
...             return 'no hiring found'
... 


```>>> get_text('Example Co. is hiring example staff for example')
' example staff for example'
>>> get_text('Example Co. is HIRING example staff for example')
' example staff for example'
>>> get_text('Example Co. is example staff for example')
'no hiring found'
>>> get_text('HIRING is example staff for example')
' is example staff for example'
>>> get_text('example')
'no hiring found'
>>> get_text('')
'no hiring found'
>>> 

Provided you want to keep the case you can modify the function to make a lower case copy and check in that. See below.

>>> def second_method(text):
...     textcopy = text.lower()
...     if 'hiring' in textcopy:
...             return text[textcopy.index('hiring') + len('hiring'):]
... 
>>> second_method('something HIRing Some Thing YGGUN jb')
' Some Thing YGGUN jb'
>>> 
Related