Re: check to see if value can be an integer instead of string



On Tue, 17 Jan 2006 18:02:52 -0800, nephish wrote:

> Hello there,
> i need a way to check to see if a certain value can be an integer. I
> have looked at is int(), but what is comming out is a string that may
> be an integer.

Not in Python it isn't. int(value) returns an int, not a string.

> i mean, it will be formatted as a string, but i need to
> know if it is possible to be expressed as an integer.
>
> like this
>
> var = some var passed to my script
> if var can be an integer :
> do this
> else:
> change it to an integer and do something else with it.


So, let's see if I understand your problem:

if var can be an integer, you "do this" (whatever this is).

If var *can't* be an integer, you change it to an integer anyway.



> whats the best way to do this ?


try:
int(var)
except ValueError:
raise
else:
do_this()


Or even simpler:

int(var)
do_this()


--
Steven.

.



Relevant Pages

  • (patch for Bash) regex conditional tests
    ... 'regex' are returned in array variable SUBMATCH. ... Skipping of positional parameters, array elements, string ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)
  • Re: Exception handling suggestions
    ... And as far as int parsing try something like this ... /// Safely casts a string as integer. ... public static int intFromString(string var) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: check to see if value can be an integer instead of string
    ... > have looked at is int, but what is comming out is a string that may ... The "int" builtin function never returns any value but an integer. ... > var = some var passed to my script ...
    (comp.lang.python)
  • Re: Passing const int * to a function
    ... For var to be constant the declaration of f should be ... int f ... int f (const int *const var) ... Bounce first left and then right and then continue in this same ...
    (comp.lang.c)
  • Re: template for function pointer
    ... > void multi(double* arrayPtr, int len){ ... > var is defined. ... > incoming array with the double value. ... > is not a function pointer, it will never go into case 2 in the switch ...
    (comp.lang.cpp)

Loading