Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is there any way to understand what data type that a string holds... The question is of little logic but see below cases

varname = '444'
somefunc(varname) => int

varname = 'somestring'
somefunc(varname) => String

varname = '1.2323'
somefunc(varname) => float

My Case: I get a mixed data in a list but they're in string format.

myList = ['1', '2', '1.2', 'string']

I'm looking for a generic way to understand whats their data so that i can add respective comparison. Since they're already converted to string format, I cant really call the list (myList) as mixed data... but still is there a way?

share|improve this question
I tried type(eval('123')) => int, but type(eval('somename')) would fail as it cant be evaluated... also would work wrong if there is a variable named somename – Prasath Jul 12 at 21:57
1  
If you have just a few types to test, like the basic data types and string, then yes (just try to cast them). Otherwise, no. – tobias_k Jul 12 at 21:57
1  
You should really edit your OP instead of commenting on it – inspectorG4dget Jul 12 at 21:58
1  
Try ast.literal_eval – Jon Clements Jul 12 at 21:58
@JonClements: most eval calls are inherently very dangerous, no? – inspectorG4dget Jul 12 at 21:58
show 3 more comments

3 Answers

up vote 14 down vote accepted
from ast import literal_eval

def str_to_type(s):
    try:
        k=literal_eval(s)
        return type(k)
    except:
        return type(s)


l = ['444', '1.2', 'foo', '[1,2]', '[1']
for v in l:
    print str_to_type(v)

Output

<type 'int'>
<type 'float'>
<type 'str'>
<type 'list'>
<type 'str'>
share|improve this answer
Will work for most "primitive" types. Won't work for anything more complex, such as trying to get the type of a variable which happens to be a function object (like a lambda). – 2rs2ts Jul 12 at 22:07
this code thinks it's a string – perreal Jul 12 at 22:08
Thanks perreal!!! This would work for my current data. – Prasath Jul 12 at 22:27

You can use ast.literal_eval() and type():

import ast
stringy_value = '333'
try:
    the_type = type(ast.literal_eval(stringy_value))
except:
    the_type = type('string')
share|improve this answer
2  
Won't work on strings. – 2rs2ts Jul 12 at 22:04
1  
That's true actually, see my edit. – murftown Jul 12 at 22:06
1  
You're good to go now :) – 2rs2ts Jul 12 at 22:08
1  
Thanks murftown!!! this is exactly what i'm looking for!!! – Prasath Jul 12 at 22:28
Glad to help, Prasath. Pretty much the same as perreal's answer. – murftown Jul 12 at 22:55

I would just try different types, in the right order:

>>> def detect(s):
...     try:
...         return type(int(s))
...     except (TypeError, ValueError):
...         pass
...     try:
...         return type(float(s))
...     except (TypeError, ValueError):
...         pass
...     return type(s)
... 
>>> detect('3')
<type 'int'>
>>> detect('3.4')
<type 'float'>
>>> detect('foo')
<type 'str'>
share|improve this answer
2  
Caveat: You have to do int before float otherwise you could do float(1) and it'd be just fine. – 2rs2ts Jul 12 at 22:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.