Python Print Type of Variable – How to Get Var Type (2024)

/ #Python
Python Print Type of Variable – How to Get Var Type (2)

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:

type(variableName)

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 <class ‘string‘> 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.99lessons = ["RWD", "JavaScript", "Databases", "Python"]person = { "firstName": "John", "lastName": "Doe", "age": 28}langs = ("Python", "JavaScript", "Golang")basics = {"HTML", "CSS", "JavaScript"}

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: <class 'str'># The variable, score is of type: <class 'float'> # The variable, lessons is of type: <class 'list'># The variable, person is of type: <class 'dict'> # The variable, langs is of type: <class 'tuple'> # The variable, basics is of type: <class 'set'>

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.

Thank you for reading.

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

Python Print Type of Variable – How to Get Var Type (3)
Kolade Chris

I'm a software developer and tech writer focusing on frontend technologies

If you read this far, thank the author to show them you care.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

ADVERTIsem*nT

Python Print Type of Variable – How to Get Var Type (2024)
Top Articles
Ultimate AdMob Monetization Guide that you need in 2024
5.2 Self-Defense – Criminal Law
Star Wars Mongol Heleer
Ron Martin Realty Cam
Fan Van Ari Alectra
Kathleen Hixson Leaked
OSRS Fishing Training Guide: Quick Methods To Reach Level 99 - Rune Fanatics
Meg 2: The Trench Showtimes Near Phoenix Theatres Laurel Park
Rochester Ny Missed Connections
Over70Dating Login
Https E24 Ultipro Com
Fool’s Paradise movie review (2023) | Roger Ebert
Find Such That The Following Matrix Is Singular.
Lazarillo De Tormes Summary and Study Guide | SuperSummary
Sni 35 Wiring Diagram
The Menu Showtimes Near Regal Edwards Ontario Mountain Village
Concordia Apartment 34 Tarkov
Tu Pulga Online Utah
Why do rebates take so long to process?
Is Windbound Multiplayer
1973 Coupe Comparo: HQ GTS 350 + XA Falcon GT + VH Charger E55 + Leyland Force 7V
What Individuals Need to Know When Raising Money for a Charitable Cause
The Boogeyman (Film, 2023) - MovieMeter.nl
Hdmovie2 Sbs
They Cloned Tyrone Showtimes Near Showbiz Cinemas - Kingwood
Lcsc Skyward
Imagetrend Elite Delaware
Craigslist Sf Garage Sales
Sinai Sdn 2023
Chapaeva Age
Wow Quest Encroaching Heat
Xemu Vs Cxbx
Craigslist West Seneca
Flashscore.com Live Football Scores Livescore
Tugboat Information
Www Craigslist Com Brooklyn
SF bay area cars & trucks "chevrolet 50" - craigslist
Mississippi weather man flees studio during tornado - video
LoL Lore: Die Story von Caitlyn, dem Sheriff von Piltover
Pink Runtz Strain, The Ultimate Guide
R: Getting Help with R
Poe Self Chill
Mother Cabrini, the First American Saint of the Catholic Church
Doe mee met ons loyaliteitsprogramma | Victoria Club
The Complete Uber Eats Delivery Driver Guide:
Meet Robert Oppenheimer, the destroyer of worlds
Take Me To The Closest Ups
Puss In Boots: The Last Wish Showtimes Near Valdosta Cinemas
Image Mate Orange County
Sj Craigs
Compete My Workforce
Supervisor-Managing Your Teams Risk – 3455 questions with correct answers
Latest Posts
Article information

Author: Velia Krajcik

Last Updated:

Views: 6089

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Velia Krajcik

Birthday: 1996-07-27

Address: 520 Balistreri Mount, South Armand, OR 60528

Phone: +466880739437

Job: Future Retail Associate

Hobby: Polo, Scouting, Worldbuilding, Cosplaying, Photography, Rowing, Nordic skating

Introduction: My name is Velia Krajcik, I am a handsome, clean, lucky, gleaming, magnificent, proud, glorious person who loves writing and wants to share my knowledge and understanding with you.