Signup/Sign In
LAST UPDATED: JUNE 26, 2023

How to find Variable type in Python?

Technology #python

    In this article, we will learn how to find the type of a variable in Python no matter what is stored in that variable. The variable can have a list, some class object, string, number, tuple, or anything else.

    We use the type() function to find the type of any variable in Python.

    How to find Variable type in Python?

    Python type() Function

    This function has a very simple syntax and can be used to find the type of any variable in Python be it a collection type variable, a class object variable, or a simple string or integer. Below is the syntax,

    type(<VARIABLE_NAME>)

    Let's take an example for different datatypes and see the output.

    a = 12
    print(type(a))

    Output:

    <class 'int'>
    

    Let's take another example,

    a = 1000000000
    print(type(a))

    Output:

    <class 'int'>

    In Python 2.x version, the output for the above code will be <class 'long'>, but from Python 3.x onwards, there is only one datatype which is int, which is equivalent to long of python 2.x version.

    For a literal sequence of characters or a string variable,

    a = 'Hello World!'
    print(type(a))

    Output:

    <class 'str'>
    

    If you want to get the name of the datatype only then you can use the __name__ attribute along with the type() function. Below we have an example for that too.

    a = '3.14159'
    print(type(a).__name__)

    Output:

    'float'
    

    The code examples till now were about basic datatypes, now let's see a few code examples for list, tuples, etc.

    my_list = [1, 2, 3, 4]
    my_tuple = (1, 2, 3, 4)
    
    print(type(my_list))
    print(type(my_tuple))
    

    Output:

    <class 'list'>
    <class 'tuple'>
    

    Using type() function for User-defined Class Objects

    Now let's try and use the type() function for identifying the type for any object of a custom class and see if it returns the correct value.

    class Studytonight:
      # init method
      def __init__(self):
        print("Object initialised")
        
    st = Studytonight()
    print(type(st))

    Output:

    Object initialised
    <class '__main__.Studytonight'>

    So, we can use the type() function to find the type of all kinds of variable objects.

    Conclusion

    We have explored various methods to find variable types in Python. From using the type() function to leveraging the isinstance() function and the introspection capabilities of the built-in inspect module, we have covered recommended approaches for type identification. Additionally, we have discussed the importance of understanding Python's dynamic typing system and the potential challenges it presents.

    As you continue your Python programming journey, remember to leverage the techniques discussed in this article to accurately determine variable types. Understanding the data you are working with is key to writing robust, maintainable, and efficient code.

    If you face any problem while executing any of the above code examples, or if have any doubts, post them in the comments. We would be more than happy to help.

    Frequently Asked Questions(FAQs)

    1. How can I determine the type of a variable in Python?

    You can use the type() function to determine the type of a variable in Python. For example, type(my_variable) will return the type of the variable my_variable.

    2. Can I check if a variable is of a specific type in Python?

    Yes, you can use the isinstance() function to check if a variable is of a specific type. It takes two arguments: the variable and the type you want to check. For example, isinstance(my_variable, int) will return True if my_variable is an integer.

    3. Are there any libraries in Python that assist with type identification?

    Yes, Python's built-in inspect module provides functions like inspect.ismodule(), inspect.isfunction(), and inspect.isclass(), which allow for more advanced type identification and introspection.

    4. Does Python have a feature similar to static typing or type annotations?

    Yes, Python 3 introduced the concept of type annotations, allowing you to hint the expected types of variables. However, type annotations are not enforced by the interpreter and serve primarily as documentation and tooling aids.

    5. Can the type of a variable change during runtime in Python? A5: Yes, Python is a dynamically typed language, which means the type of a variable can change during runtime. Variables can be reassigned to values of different types, and function arguments can accept values of different types.

    You may also like:

    This is the official profile of the Studytonight content team.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS