How do I check if raw input is integer in python 2.7?

Alejandro Veintimilla

Is there a method that I can use to check if a raw_input is an integer?

I found this method after researching in the web:

print isinstance(raw_input("number: ")), int)

but when I run it and input 4 for example, I get FALSE. I'm kind of new to python, any help would be appreciated.

falsetru

isinstance(raw_input("number: ")), int) always yields False because raw_input return string object as a result.

Use try: int(...) ... except ValueError:

number = raw_input("number: ")
try:
    int(number)
except ValueError:
    print False
else:
    print True

or use str.isdigit:

print raw_input("number: ").isdigit()

NOTE The second one yields False for -4 because it contains non-digits character. Use the second one if you want digits only.

UPDATE As J.F. Sebastian pointed out, str.isdigit is locale-dependent (Windows). It might return True even int() would raise ValueError for the input.

>>> import locale
>>> locale.getpreferredencoding()
'cp1252'
>>> '\xb2'.isdigit()  # SUPERSCRIPT TWO
False
>>> locale.setlocale(locale.LC_ALL, 'Danish')
'Danish_Denmark.1252'
>>> '\xb2'.isdigit()
True
>>> int('\xb2')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '\xb2'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I check an input for an integer in Python?

From Dev

How can I check input is integer, while using FOR for RAW_INPUT ..in Python?

From Dev

how to check whether raw_input is integer, string and date in python

From Dev

How do I check to see if the input is an integer?

From Dev

How do I check if input is a fraction (Python)?

From Dev

How do I check if input is a number in Python?

From Dev

How do I check if input is a number in Python?

From Java

How do I use raw_input in Python 3

From Dev

How do I check if an input is a string or int in Python 2.x?

From Dev

How do I check input variable validity from a Python extension?

From Dev

How do I check an input answer in python 3

From Dev

How do I make this program check if an input is an integer without causing an error in runtime and stop the loop with a sentinel value?

From Java

How do I check that a number is float or integer?

From Dev

How do i validate integer as user input?

From Dev

How do I get a UITextField input as an Integer

From Dev

How do you check to see if an input is equal to a certain integer or integers?

From Dev

How can I check if an input is a integer or String, etc.. in JAVA?

From Dev

How do I check when an input is changed?

From Dev

How do I check if input field is in focus or not?

From Dev

How do i check if input is a date

From Dev

How to check the input is an integer or not in Java?

From Dev

How do I check if 2 numbers are equal to a specific ratio in Python?

From Dev

How do I check if 2 keys are released at the same time in Python?

From Dev

How do I check if 2 numbers are equal to a specific ratio in Python?

From Dev

How do I make my program check whether user input is within 5,10,15,20, etc of the random integer?

From Dev

How do I give an invalid message for strings that cannot be converted to an integer, via the input() function in Python?

From Dev

How do I find all positive integer solutions to 7x+2y=n

From Dev

How do you check if input is numerical in Python?

From Dev

How to check if input/raw_input contains an item in a list. Python code

Related Related

  1. 1

    How do I check an input for an integer in Python?

  2. 2

    How can I check input is integer, while using FOR for RAW_INPUT ..in Python?

  3. 3

    how to check whether raw_input is integer, string and date in python

  4. 4

    How do I check to see if the input is an integer?

  5. 5

    How do I check if input is a fraction (Python)?

  6. 6

    How do I check if input is a number in Python?

  7. 7

    How do I check if input is a number in Python?

  8. 8

    How do I use raw_input in Python 3

  9. 9

    How do I check if an input is a string or int in Python 2.x?

  10. 10

    How do I check input variable validity from a Python extension?

  11. 11

    How do I check an input answer in python 3

  12. 12

    How do I make this program check if an input is an integer without causing an error in runtime and stop the loop with a sentinel value?

  13. 13

    How do I check that a number is float or integer?

  14. 14

    How do i validate integer as user input?

  15. 15

    How do I get a UITextField input as an Integer

  16. 16

    How do you check to see if an input is equal to a certain integer or integers?

  17. 17

    How can I check if an input is a integer or String, etc.. in JAVA?

  18. 18

    How do I check when an input is changed?

  19. 19

    How do I check if input field is in focus or not?

  20. 20

    How do i check if input is a date

  21. 21

    How to check the input is an integer or not in Java?

  22. 22

    How do I check if 2 numbers are equal to a specific ratio in Python?

  23. 23

    How do I check if 2 keys are released at the same time in Python?

  24. 24

    How do I check if 2 numbers are equal to a specific ratio in Python?

  25. 25

    How do I make my program check whether user input is within 5,10,15,20, etc of the random integer?

  26. 26

    How do I give an invalid message for strings that cannot be converted to an integer, via the input() function in Python?

  27. 27

    How do I find all positive integer solutions to 7x+2y=n

  28. 28

    How do you check if input is numerical in Python?

  29. 29

    How to check if input/raw_input contains an item in a list. Python code

HotTag

Archive