How to Check the Type of a Variable in Python Using the Print Function?

Estimated read time 1 min read

You can check the type of a variable in Python using the print() function by passing the result of the type() function as an argument to the print() function. Here’s an example:

x = 42
print(type(x))

In the above code, the type() function is used to get the type of the x variable, which is an integer. Then, the print() function is used to print the result of the type() function, which will output the following:

<class 'int'>

Similarly, you can check the type of any other variable in Python by passing it as an argument to the type() function and then passing the result to the print() function.

y = "Hello, World!"
print(type(y))

This will output:

<class 'str'>

By using the print() function with the type() function, you can easily check the type of any variable in your Python code.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply