Type check for mixture of integer and strings

Viewed 53

I'm trying to do a type check for my program I know I can check if input in tkinter is an integer using-

if name_write.isdigit():
 print("Input valid requirement")

However, how do I do a check for a user input that contains integers and strings? For example, what if the user put in "J22hn" How do I check for that?

1 Answers

So a string is merely a sequence of characters. An int is valid integer number less than the maximum value. Therefore, when you say user input that "... contains integers and strings..." I think what you mean is that it contains "digits" and "letters." Both letters and digits are characters. So if you are looking for to see if the incoming input has letters and/or digits, you need to iterate over the characters in the string.

You can use code like this to check and see if you have letters and/or digits in the input string.

inputString = "J22hn"
hasDigit = False
hasLetter = False
for character in inputString:
     if character.isalpha():
          hasLetter = True
          continue
     if character.isdigit():
          hasDigit = True
          continue
# Do whatever else you need to do here with that info. 
Related