【问题标题】:How to check which type of data stored in a variable in python? [duplicate]如何检查python中变量中存储的数据类型? [复制]
【发布时间】:2018-04-27 00:29:41
【问题描述】:

我对 Python 很陌生。有什么方法可以找出特定变量中存储的值类型吗?

在下面的代码中如何找出变量中存储值的类型?

counter = 100          
miles   = 1000.0       
name    = "John"     
name    =10


print counter
print miles
print name

【问题讨论】:

标签: python


【解决方案1】:
>>> type('1')
<class 'str'>
>>> type(1)
<class 'int'>
>>> type(1.000)
<class 'float'>
>>> type({1:2})
<class 'dict'>
>>> type([1,2,3])
<class 'list'>

【讨论】:

    【解决方案2】:

    很简单,使用python内置的type命令。

    例如,您可以检查此变量的类型:

    >>> name = 'John'
    >>> print(type(name))
    <class 'str'>
    

    在变量的任何地方使用此方法,您不必打印它。例如,您可以根据变量的类型使用它对不同的变量执行不同的操作。

    希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 2012-05-12
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 2017-01-05
      • 2020-10-06
      • 2017-06-18
      相关资源
      最近更新 更多