Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a variable is an integer or a string? [duplicate]

I have an application that has a couple of commands. When you type a certain command, you have to type in additional info about something/someone. Now that info has to be strictly an integer or a string, depending on the situation.

However, whatever you type into Python using raw_input() actually is a string, no matter what, so more specifically, how would I shortly and without try...except see if a variable is made of digits or characters?

like image 387
user2154354 Avatar asked May 10 '13 18:05

user2154354


People also ask

How do you know if a variable is an integer?

To check if the variable is an integer in Python, we will use isinstance() which will return a boolean value whether a variable is of type integer or not. After writing the above code (python check if the variable is an integer), Ones you will print ” isinstance() “ then the output will appear as a “ True ”.

How do you check if a string is an integer?

To check if a string is integer in Python, use the isdigit() method. The string isdigit() is a built-in Python method that checks whether the given string consists of only digits.

How do you check if a variable is an int or a string in Python?

We can use the isdigit() function to check if the string is an integer or not in Python. The isdigit() method returns True if all characters in a string are digits. Otherwise, it returns False.


2 Answers

In my opinion you have two options:

  • Just try to convert it to an int, but catch the exception:

    try:     value = int(value) except ValueError:     pass  # it was a string, not an int. 

    This is the Ask Forgiveness approach.

  • Explicitly test if there are only digits in the string:

    value.isdigit() 

    str.isdigit() returns True only if all characters in the string are digits (0-9).

    The unicode / Python 3 str type equivalent is unicode.isdecimal() / str.isdecimal(); only Unicode decimals can be converted to integers, as not all digits have an actual integer value (U+00B2 SUPERSCRIPT 2 is a digit, but not a decimal, for example).

    This is often called the Ask Permission approach, or Look Before You Leap.

The latter will not detect all valid int() values, as whitespace and + and - are also allowed in int() values. The first form will happily accept ' +10 ' as a number, the latter won't.

If your expect that the user normally will input an integer, use the first form. It is easier (and faster) to ask for forgiveness rather than for permission in that case.

like image 94
Martijn Pieters Avatar answered Oct 22 '22 21:10

Martijn Pieters


if you want to check what it is:

>>>isinstance(1,str) False >>>isinstance('stuff',str) True >>>isinstance(1,int) True >>>isinstance('stuff',int) False 

if you want to get ints from raw_input

>>>x=raw_input('enter thing:') enter thing: 3 >>>try: x = int(x)    except: pass  >>>isinstance(x,int) True 
like image 37
TehTris Avatar answered Oct 22 '22 20:10

TehTris