共計 481 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Python 中,你可以使用以下方法來打印數據類型:
-
使用
type()
函數:type()
函數可以返回一個對象的類型。x = 5 print(type(x)) # <class 'int'>
-
使用
isinstance()
函數:isinstance()
函數用于檢查一個對象是否屬于指定的類型或者多個類型。x = "Hello" print(isinstance(x, str)) # True
-
使用
__class__
屬性:通過對象的__class__
屬性也可以獲取其類型。x = [1, 2, 3] print(x.__class__) # <class 'list'>
-
使用 f-string 格式化輸出:結合
type()
函數和 f-string 可以直接打印對象的類型。x = {"name": "Alice", "age": 30} print(f"Type of x is {type(x)}") # Type of x is <class 'dict'>
這些是在 Python 中打印數據類型的幾種常見方法。你可以根據具體情況選擇適合的方法來查看數據類型。
丸趣 TV 網 – 提供最優質的資源集合!
正文完