在实际写程序中,经常要对变量类型进行判断,除了用type(变量)这种方法外,还可以用isinstance方法判断:
a = 1
b = [1,2,3,4]
c = (1,2,3,4)
d = {'a':1, 'b':2, 'c':3}
e = "abc"
if isinstance(a,int):
print ("a is int")
else:
print ("a is not int")
if isinstance(b,list):
print ("b is list")
else:
print ("b is not list")
if isinstance(c,tuple):
print ("c is tuple")
else:
print ("c is not tuple")
if isinstance(d,dict):
print ("d is dict")
else:
print ("d is not dict")
if isinstance(e,str):
print ("d is str")
else:
print ("d is not str")
更多关于python判断变量是否为int、字符串、列表、元组、字典的方法请查看下面的相关链接
您可能感兴趣的文章:Python 字符串处理特殊空格\xc2\xa0\t\n Non-breaking space基于python3实现倒叙字符串Python日期格式和字符串格式相互转换的方法通过python检测字符串的字母python如何把字符串类型list转换成listpython字符串,元组,列表,字典互转代码实例详解python字符串下标与切片及使用方法python 实现字符串下标的输出功能Python基础之字符串操作常用函数集合python字符串替换re.sub()实例解析Python如何访问字符串中的值python3 字符串知识点学习笔记Python输出指定字符串的方法python求一个字符串的所有排列的实现方法详解字符串在Python内部是如何省内存的代码总结Python2 和 Python3 字符串的区别Python字符串中删除特定字符的方法Python拼接字符串的7种方式详解