Allow Only Alpha Characters in Python?

后端 未结 5 1538
逝去的感伤
逝去的感伤 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条回答
  •  Happy的楠姐
    2020-12-12 06:56

    You might want to look into regular expressions:

    ^[A-Za-z ]*$
    

    is a regex that only matches strings if they consist entirely of ASCII letters and spaces.

    To also allow letters of your current locale:

    ^[^\W\d_]*$
    

    which is basically the set of alphanumeric characters including accented characters (in the current locale) minus digits and underscore.

提交回复
热议问题