How to Check the Type of a Value in Python?

Estimated read time 1 min read

In Python, you can check the type of a value using the type() function. Here’s an example:

x = 42
print(type(x)) # Output: <class 'int'>

In the above example, the type() function is used to check the type of the value 42, which is an integer. The output shows that the type of 42 is <class 'int'>.

Similarly, you can check the type of any other value in Python by passing it as an argument to the type() function.

y = "Hello, World!"
print(type(y)) # Output: <class 'str'>

In this example, the type() function is used to check the type of the string "Hello, World!", which is a string. The output shows that the type of "Hello, World!" is <class 'str'>.

You can use the type() function to check the type of any value in Python, including integers, strings, lists, tuples, dictionaries, and more.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply