Python print variable type

How to Check Type of Variable in Python

In this tutorial, we’ll learn about getting and testing the type of variables by using two different ways, and finally, we’ll know the difference between these two ways.

1. Checking Variable Type With Type() built-in function

what is type()

type() is a python built function that returns the type of objects

syntax

As you can see, in the above code we have many different variables,
now let’s get the type of these variables.

If you want to know all types of objects in python, you’ll find it in the final part of the article.

Example 2: checking if the type of variable is a string

let’s say that you want to test or check if a variable is a string, see the code bellow

Читайте также:  Графические движки для java

As you can see, the code works very well, but this is not the best way to do that.
Remember!, if you want to check the type of variable, you should use isinstance() built function.

2. Checking Variable Type With isinstance() built-in function

what is isinstance()

The isinstance() is a built-in function that check the type of object and return True or False

sytnax

example 1: checking variables type

 #check if "list_var" is list print(isinstance(list_var, list)) #check if "string_var" is tuple print(isinstance(string_var, tuple)) #check if "tuple_var" is tuple print(isinstance(tuple_var, tuple)) #check if "dictionry_var" is dictionary print(isinstance(dictionry_var, dict)) 

if you want to get type of object use type() function.

if you want to check type of object use isinstance() function

4. Data Types in Python

Источник

Python Print Type of Variable – How to Get Var Type

Kolade Chris

Kolade Chris

Python Print Type of Variable – How to Get Var Type

If you’re a Python beginner, becoming familiar with all its various data types can be confusing at first. After all, there are a lot of them available to you in the language.

In this article, I’m going to show you how to get the type of various data structures in Python by assigning them to a variable, and then printing the type to the console with the print() function.

How to Print the Type of a Variable in Python

To get the type of a variable in Python, you can use the built-in type() function.

The basic syntax looks like this:

In Python, everything is an object. So, when you use the type() function to print the type of the value stored in a variable to the console, it returns the class type of the object.

For instance, if the type is a string and you use the type() on it, you’d get as the result.

To show you the type() function in action, I’m going to declare some variables and assign to them the various data types in Python.

name = "freeCodeCamp" score = 99.99 lessons = ["RWD", "JavaScript", "Databases", "Python"] person = < "firstName": "John", "lastName": "Doe", "age": 28 >langs = ("Python", "JavaScript", "Golang") basics =

I will then print the types to the console by wrapping print() around some strings and the type() function.

print("The variable, name is of type:", type(name)) print("The variable, score is of type:", type(score)) print("The variable, lessons is of type:", type(lessons)) print("The variable, person is of type:", type(person)) print("The variable, langs is of type:", type(langs)) print("The variable, basics is of type:", type(basics)) 

Here are the outputs:

# Outputs: # The variable, name is of type: # The variable, score is of type: # The variable, lessons is of type: # The variable, person is of type: # The variable, langs is of type: # The variable, basics is of type:

Final Thoughts

The type() function is a valuable built-in function of Python with which you can get the data type of a variable.

If you’re a beginner, you should save yourself the hassle of cramming data types by using the type() function to print the type of a variable to the console. This will save you some time.

You can also use the type() function for debugging because in Python, variables are not declared with data types. So, the type() function was built into the language for you to check for data types of variables.

Источник

Python Print Type of Variable – How to Get Variable Type

To print a type of variable in Python, you can use the “type()” along with the “print()” function. For example, print(type(“ABC”)) returns . The type() function returns the variable’s data type.

Syntax

Parameters

The function returns its type if the single object is passed to type().

Example

Let’s print the different types of variables.

str = "Welcome to Guru99" run = 100 rr = 7.7 complex_num = 19j+21 player_list = ["VK", "RS", "JB", "RP"] bat_name = ("A", "B", "C", "D") duckworth = lbw = print("The type is : ", type(str)) print("The type is : ", type(run)) print("The type is : ", type(rr)) print("The type is : ", type(complex_num)) print("The type is : ", type(player_list)) print("The type is : ", type(bat_name)) print("The type is : ", type(duckworth)) print("The type is : ", type(lbw))
The type is : The type is : The type is : The type is : The type is : The type is : The type is : The type is : 

You can see that we printed the different data types of different variables.

Using isinstance() method

The isinstance() is a built-in Python method that returns True if a specified object is of the specified type. The isinstance() method accepts two arguments: object and classtype and returns True if the defined object is of the defined type.

Syntax

isinstance(object, classtype)

Arguments

object: It is an object whose instance you compare with class type. It will return True if the type matches; otherwise false.

class type: It is a type, class, or a tuple of types and classes.

Example

In this example, we will compare the float value with type float, i.e., the 19.21 value will be compared with type float.

fvalue = 19.21 dt = isinstance(fvalue, float) print("It is a float value:", dt)

And it returns True.

Using the isinstance() method, you can test for string, float, int, list, tuple, dict, set, and class.

How to print two variables in Python

To print the two variables in Python, use the print() function. The “%s” in print(“%s”) represents a String or any object with a string representation, like numbers.

name = "Stranger Things" season = 5 print("Total seasons for %s is %s" % (name, season))
Total seasons for Stranger Things are 5

2 thoughts on “Python Print Type of Variable – How to Get Variable Type”

Hi,
I just started to learn Python.
I have variables:
x=1j
y=2.2
Now, I would like to print them in a single print statement on two-line output, to show something like this:
X = 1j , , with comment1
Y = 2.3, , with comment2
Your advice would be greatly appreciated.
Cheers,
Zoltan. Reply

In the printout I would like to show the type() of the given variable as well. (I wrote it but
only two commas appeared instead.) Reply

Leave a Comment Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Источник

How To Print Type Of Variable In Python

How To Print Type Of Variable In Python

The type() method in Python can be used to print the type of a variable. type() is a Python built-in function that returns the variable’s data type. In Python, use the print() method to print the variables.

How To Print type of variable

The type() method in Python can be used to determine a variable’s data type. The type() method returns the argument(object) supplied as a parameter’s class type. It will return output with the class if the input is a list, if the input is a string, and so on.

Syntax:
type(object)

Parameters

A single object is passed to type().

Example :

Let’s print the different types of variables.

str = "Welcome to pythonpip" age = 34 salary = 7342.7 complex_num = 11j+21 a_list = ["name", "age", "salary"] emps = ("A", "B", "C") a_dict = print("The type is : ", type(str)) print("The type is : ", type(age)) print("The type is : ", type(salary)) print("The type is : ", type(complex_num)) print("The type is : ", type(a_list)) print("The type is : ", type(emps)) print("The type is : ", type(a_dict))
The type is : The type is : The type is : The type is : The type is : The type is : The type is : ** Process exited - Return Code: 0 ** Press Enter to exit terminal

Python isinstance

The isinstance() is a built-in Python method that returns True if a specified object is of the specified type. The isinstance() method accepts two arguments: object and classtype and returns True if the defined object is of the defined type.

Syntax :

Arguments:

object: You’re comparing the instance of an object to the class type. If the type matches, it will return True; otherwise, it will return False.

class type: It is a type or a class or a tuple of types and classes.

Example

Let’s compare the float value with type float, i.e., 13.15 value will be compared with type float.

fvalue = 13.15 inst_t = isinstance(fvalue, float) print("Instance Type:", inst_t)

Output :
Instance Type: True

The above code returns True value.

Источник

Оцените статью