>>> type(33) == types.IntType
True
>>> type("33") == types.StringType
True
>>> type(33.33) == types.FloatType
True
>>> type({"dd":33}) == types.DictType
True
>>> type([33,33]) == types.ListType
True
>>> type((33,33)) == types.TupleType
True
>>> type({"33","22"}) == set #没有types.SetType类型
True
>>> class A(object):
... pass
...
>>> type(A()) == types.ObjectType
False
>>>type(22) == int
True
>>> type("test") == str
True
>>> type(22.33) == float
True
>>> type({"a":2}) == dict
True
>>> type((3,32,2)) == tuple
True
>>> type([2,2,3,4]) == list
True
>>> class A(object):
... pass
...
>>> type(A()) == A
True
>>> type(A()) == object # 不会认为子类是一种父类类型
False
>>> isinstance(A(), object) # 会认为子类是一种父类类型
True
>>> isinstance(22,int)
True
>>> isinstance("33", str)
True
>>> isinstance(22.22, float)
True
>>> isinstance({"aa":22}, dict)
True
>>> isinstance([22,22,33], list)
True
>>> isinstance((33,33,44), tuple)
True
>>> isinstance({"33","34"},set )
True
>>> class A:
... pass
...
>>> isinstance(A(), A)
True
>>> isinstance(A(), object)
True
>>> type(A()) == object
False
>>> type(A()) == A
False
>>> type(A()) == types.ObjectType # 不会认为子类是一种父类类型
False
结论:尽量不要使用type()方法,多使用isinstance()来判断数据类型,
type不会任务子类是一种父类类型,而isinstance认为子类是一种父类
2.python3类型判断
C:\Users\Administrator>python
Python 3.6.5 4
Type "help", "copyright", "credits" or "license" for more in
>>> import types
>>> type(33) == types.IntType # python3中不支持该类型判断方式
Traceback (most recent call last):
File "
AttributeError: module 'types' has no attribute 'IntType'
>>> type(33) == int
True
>>> type("33") == str
True
>>> type(33.33) == float
True
>>> type([3,3,3]) == list
True
>>> type({"a":23}) == dict
True
>>> type((33,3)) == tuple
True
>>> type({"33","22"}) == set
True
>>> isinstance({"33","34"},set )
True
>>> isinstance(22, int)
True
>>> isinstance(22.33, float)
True
>>> isinstance("aa", str)
True
>>> isinstance({"aa":33}, dict)
True
>>> isinstance([22,23], list)
True
>>> isinstance((22,23), tuple)
True
>>> class A(object):
... pass
...
>>> type(A()) == A
True
>>> type(A()) == object
False
>>> type(A()) == types.ObjectType
Traceback (most recent call last):
File "
AttributeError: module 'types' has no attribute 'ObjectType'
>>> isinstance(A(), A)
True
>>> isinstance(A(), object)
True
结论:Python3中,类型判断不能使用types.IntType等方式判断类型,尽量使用isinstance来判断类型,type不会任务子类是一种父类类型,而isinstance认为子类是一种父类
3.字符串类型判断
前提:变量Str_1为字符串
Str_1.isalnum() 所有字符都是数字或者字母,
Str_1.isalpha() 所有字符都是字母
Str_1.isdigit() 所有字符都是数字
Str_1.islower() 所有字符都是小写
Str_1.isupper() 所有字符都是大写
Str_1.istitle() 所有单词都是首字母大写
Str_1.isspace() 所有字符都是空白字符
以上方法为真返回 Ture,否则返回 False
例如:
>>> Str_1 = "333"
>>> Str_1.isdigit()
True
>>>
结论:以上字符类型判断在python2和python3中没有区别