I have following string and I want to convert it to array/list so I can measure its length.
a="abc,cde,ert,ert,eee"
b="a", "b", "c"
The expected length for a should be 1 and the expected length for b should be 3.
I have following string and I want to convert it to array/list so I can measure its length.
a="abc,cde,ert,ert,eee"
b="a", "b", "c"
The expected length for a should be 1 and the expected length for b should be 3.
 
    
    a is a string, b is a tuple. You can try something like this:
def length_of_str_or_tuple(obj):
    if(isinstance(obj,basestring)):
        return 1
    return len(obj)
Although what you're doing is really weird and you should probably rethink your approach.
 
    
    You can use something like this:
>>> a="abc,cde,ert,ert,eee"
>>> b="a", "b", "c"
>>> 1 if isinstance(a, str) else len(a)
1
>>> 1 if isinstance(b, str) else len(b)
3
>>>
In the above code, the conditional expression uses isinstance to test whether or not item is a string object.  It returns 1 if so and len(item) if not.
Note that in Python 2.x, you should use isinstance(item, basestring) in order to handle both unicode and str objects.
 
    
    There's a crude way to do this: check which is a string and which a tuple:
x ={}
for item in (a,b):
    try:
        item.find('')
        x[item] = 1
    except:
        x[item] = len(item)
Since a tuple object doesn't have an attribute find, it will raise an exception.
 
    
    To measure the length of the string:
len(a.split())
for the tuple:
len(list(b))
combine the previous answers to test for tuple or list and you would get what you want, or use:
if type(x) is tuple:    
    len(list(x))    
else:    
    a = x.split("\"")    
    len(a)
