Validating raw_input


#1

Hi,
So I wondering how do I validate that the user entered characters not not blank spaces?
using raw_input > 0 counts spaces as well.

Also, how can I phrase an if statement to include a combination of numbers and letters as a legitimate input but numbers alone are not.

Thanks


#2

raw_input returns a Unicode string, including the empty string.

>>> raw_input()
                 [Enter no input]
''
>>> 

If you do not want spaces, then either filter them (which you may not yet know how to do), or test for them.

user = raw_input()
if ' ' in user: ...

This could be inside a loop that only ends when the input is valid.

To see if a string is all digits,

user.isdigit()

To see if a string is all alphabet characters,

user.isalpha()

The former will return False if any characters other than numbers are present in the string. It does not consider a decimal point to be a digit so floats will return False, as well.

The latter will return False if any characters other than letters are present in the string.