Python isinstance function to determine whether the element is a string, int, float type

Source: Internet
Author: User

Isinstance (1, int) to  determine if it is of type int
Isinstance (1.0, float) to determine if it is float type
Isinstance (S, str) determine if it is a string type
Isinstance (A, dict) determines whether object A is a dictionary


Isinstance is an intrinsic function syntax in Python: Isinstance (object, ClassInfo) If the argument object is an instance of ClassInfo, or object is an instance of a subclass of the ClassInfo class, Returns True. If object is not a given type, the returned result is always false. If ClassInfo does not represent a class (type Object), then it is either a tuple of a class or recursively contains such a tuple of data types. Other sequence types are not allowed. If ClassInfo is not a data type or a tuple of data types, a TypeError exception is thrown.Example:>>> isinstance (1, int) determines whether the int type true>>> isinstance (1.0, float) determines whether it is float type true>>> s = ' abc ' & Gt;>>isinstance(s, str) Determine if it is a string type True>>>isinstance (a,dict) Determines whether object A is a dictionary, if true, prints true, such as false, to print false.

Title: The number of strings passed into the function, [number], [Word], [space], and [other], and return the result

# haha= "A b c_$123" haha=[1, "a", "a", [1,2,3],{"name": "AAA", "pwd": "111"}]s=0 # string L=0 # list N=0 # number D=0 # dictionary q=0 # Other Def f_c (a) :    for I in A:        print (Type (i))        if isinstance (i, str):            print (' is a string ')            global s            s+=1        elif Isinstance (i, list):            print (' is List ')            global l            L + = 1        elif isinstance (i, int):            print (' is number ')            Global n            n+=1        elif isinstance (i,dict):            print (' is Dictionary ')            global d            D + = 1        else:            print ( ' Other ')            global Q            q+=1f_c (haha) print ("String:%s, List:%s, Number:%s, Dictionary:%s, Other:%s."% ( S,L,N,D,Q))

  

Python isinstance function to determine whether the element is a string, int, float type

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.