ITPub博客

首页 > IT职业 > IT职场 > python怎么判断是否为字符

python怎么判断是否为字符

IT职场 作者:touchfeeling 时间:2021-09-11 10:23:48 0 删除 编辑

python中,判断某变量的数据类型是否为字符串,可用isinstance()函数,也可通过比较 type(x) == type(‘a’)的值来判断。

A = 123
B = [123.'A']
C = {'A':123,'B':45}
D = 1.23
E = 'abc'

isinstance(A, int)    #True
isinstance(B, list)   #True
isinstance(C, dict)   #True
isinstance(D, float)  #True
isinstance(E, str)    #True

type(A)== type(1)     #True
type(B)== type([])    #True

推荐学习《python教程》。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1451/viewspace-2834869/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    105
  • 访问量
    1220