9

In a function in Django, the user can send me a number or a string, and I want to know if I received a number or a String (Tip: The number will always be an integer between 1-6)

I want to know if it's possible to detect this and how (with an example), as the number or string I'm getting will tell me what to do next.

4
  • How is the number sent? In a request? Commented Nov 15, 2013 at 14:30
  • Like this: def make_choice(request, option): Commented Nov 15, 2013 at 14:33
  • Then option is always a string. Commented Nov 15, 2013 at 14:34
  • So, trying to convert it into an integer like int(option) will only work if it's a number originally. Am I wrong? Commented Nov 15, 2013 at 14:38

3 Answers 3

11

You can try to convert the string to a number using int(), catching the exception:

def isNum(data):
    try:
        int(data)
        return True
    except ValueError:
        return False

This returns True only if the string can be converted to an integer number.

Sign up to request clarification or add additional context in comments.

2 Comments

@Christian: That is what exceptions are for. Not just to detect coding errors. And no, this is not slow, this is actually faster than the alternatives, which is to use a regular expression or a series of string method tests.
@Christian: Can you show me anything other than "It's probably slow"? Because that's not much of an argument; do you have proof?
7

What about: if isinstance(data, int):

1 Comment

For folks who are looking to identify numbers which could be represented as strings: please note that this won't work for you. isinstance('7', int) will return False
2

I'm assuming your number will still be encased in a string, ie. "1" or "4" or "6" - if that's the case, then there are several ways to do it; you could use a regex to check whether it is a number or not, or you could knock up a function that would look something like this

def isNumber(your_input_string):
    return len(your_input_string) == 1 and your_input_string in "123456"

Since the number will always be between 1 and 6, it can only be a string of length 1, and it must be contained in the string '123456', since... well, those are the allowed numbers.

EDIT:

As pointed by Martijn Pieters in the comments below, that is a roundabout way of doing it; an easier solution would be just to check whether the string is between '1' or '6'.

def isNumber(your_input_string):
    return len(your_input_string) == 1 and '1' <= your_input_string <= '6'

4 Comments

You can simply write that as return len(your_input_string) == 1 and your_input_string in "123456" and there are no true and false in python
This is rather... round-about. return len(your_input_string) == 1 and '1' <= your_input_string <= '6' would do the same, but faster.
- I can't believe I overlooked the fact that I'm basically just returning a boolean expression. That's a bit embarassing. It's been a while since I last programmed anything in python, so I wasn't even sure if my syntax was correct. I didn't even remember that you could do comparisons with <= on strings - that's brilliantly elegant solution.
You do need to test for the length here; '1' <= '16' <= '6' is True as well, due to lexicographic ordering.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.