Allow Only Alpha Characters in Python?

后端 未结 5 1557
逝去的感伤
逝去的感伤 2020-12-12 06:24

I am writing a program, part of it requires the input of a name. The name can only be alpha and spaces (eg. John Smith) it cannot be John Smith1 If an invalid character IS

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-12 06:46

    To Check the string for alpha I generally use this one. Hope this can help you.

    import re
    def is_alpha_space(name):
        name_nospace = ''.join((name.split()))
        if re.search(r"\W", name_nospace) or len(name_nospace) < 1:
            return False
        return True
    name = "Mark Zumkoff"
    print(is_alpha_space(name))  # True
    name = "Mark Zumkoff@"
    print(is_alpha_space(name))  # False
    name = "    "
    print(is_alpha_space(name))  # False
    

提交回复
热议问题