How to print types within a list

前端 未结 6 1699
自闭症患者
自闭症患者 2021-01-03 02:01

So I was given a list and I must print the type of each item in the list. I can clearly see that there are strings and integers but I need it to print out in Python. We just

6条回答
  •  一向
    一向 (楼主)
    2021-01-03 02:33

     a=[12,"string"]
     for i in a: #sets the value of a to be any item in list (a)
          print(type(a), end="   ")
    

    OUTPUT:

    >>>      
    

    In the python code above type() function is used to determine the data type of variable (i). And end() is used to replace the end of the output with the value inside its' brackets () instead of a new line as you must have observed in the above output that between the 2 outputs "____ " (space) was placed without a new line.

提交回复
热议问题