Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to extract words from a sentence and check if a word is not there in it

this is my code below. I need to take an input from the user (a sentence) and check whether there are any numbers in it from 0 to 10 in it or not. I know there are many ways to approach it, e.g. split(), isalnum(), etc. but I just need help in putting it all together. Please find my code below:

sentence1 = input("Enter any sentence (it may include numbers): ")
numbers = ["1","2","3","4","5","6","7","8","9","10","0"]

ss1 = sentence1.split()
print(ss1)
if numbers in ss1:
  print("There are numbers between 0 to 10 in the sentece")
else:
  print("There are no numbers in the sentence”)

Thanks 🙂

Edit: Expected Output should be like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Enter any sentence (it may include numbers): I am 10 years old
There are numbers between 0 and 10 in this sentence

>Solution :

I would use a regex:

def contains_nums(s):
    import re
    m = re.search('\d+', s)
    if m:
        return f'"{s}" contains {m.group()}'
    else:
        return f'"{s}" contains no numbers'

Examples:

>>> contains_nums('abc 10')
'"abc 10" contains 10'

>>> contains_nums('abc def')
'"abc def" contains no numbers'

NB. this is only checking the first number, use re.findall if you need all. Also this is finding numbers within words, if you want separate numbers only, use a regex with word boundaries (\b\d+\b), finally, if you want to restrict to 0-10 numbers, use (?<!\d)1?\d(?!\d) (or \b(?<!\d)1?\d(?!\d)\b for independent numbers)

more complete solution
def contains_nums(s, standalone_number=False, zero_to_ten=False):
    import re
    
    regex = r'(?<!\d)1?\d(?!\d)' if zero_to_ten else '\d+'
    
    if standalone_number:
        regex = r'\b%s\b' % regex
    
    m = re.search(regex, s)
    if m:
        return f'"{s}" contains {m.group()}'
    else:
        a = "standalone " if standalone_number else ""
        b = "0-10 " if zero_to_ten else ""
        return f'"{s}" contains no {a}{b}numbers'
>>> contains_nums('abc100', standalone_number=False, zero_to_ten=False)
'"abc100" contains 100'

>>> contains_nums('abc100', standalone_number=True, zero_to_ten=False)
'"abc100" contains no standalone numbers'

>>> contains_nums('abc 100', standalone_number=True, zero_to_ten=True)
'"abc 100" contains no standalone 0-10 numbers'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading