pythonstringsimplify

Simplify a funcyion that checks if in string there are numbers or spaces


I must create a function that checks if in a string there's a space or a number, and if in this case return false.

I created this code but I feel that is too long, even if it works:

def is_only_string(value):
    if " " in value or "1" in value or "2" in value or "3" in value or "4" in value or "5" in value or "6" in value or "7" in value or "8" in value or "9" in value or "0" in value :
        return False
    else:
        return True

Solution

  • If you want to use a loop you could do:

    st = "abdgtss dsg"
    
    
    def check_for_num(st):
        print(list(st))
        check = False
        for item in list(st):
            if item.isnumeric() or item == " ":
                check = True
                break
        return check
    
    
    print(check_for_num(st))