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
How to Scare an Insurance Adjuster
Discuss what you would do in the situations described below. Remember
I brought the best part of Windows to my MacBook and it gave me a huge productivity boost
Kaiser Ncti
Stone-Ladeau Funeral Home | Winchendon, Massachusetts
Dass Slumber Party Pt1
Happy Ending Massage Milwaukee
Craigslist Carmel Cars For Sale By Owner
Byrn Funeral Home Mayfield Kentucky Obituaries
Varsity Tutors, a Nerdy Company hiring Hoboken Vietnamese Tutor in Hoboken, NJ | LinkedIn
Dfw To Anywhere Google Flights
Jc Green Obits
What is international trade and explain its types?
Iwu Directory
Matt Severance Picks
Wakegov Property
Deluxeblondes Com
Xlauriexkimx
75 Fun Cartoon Characters that Start with Q - Smarty n'Crafty
What The Dog Doin Origin
Daftpo
Troy Eugene Wigley I Survived Ellen Halbert
Bulls set the ‘gold standard’
Subway Surfers Unblocked Wtf
Brimstone Sands Lost Easels
Carrie June Leak
Djs In The 90S
Best Breakfast Near Grand Central Station New York
Votes Of Opposition Daily Themed Crossword
South Bend Weather Underground
Walgreens Colesville
Craigslist Ct Apartments For Rent
Ludvigsen Mortuary Fremont Nebraska
Sams La Habra Gas Price
Quenisha Poole Verdict
Shiawassee County 911 Active Events
Jessica Ann Ussery Wiki
Raneka and Asonta: Are the Love After Lockup Stars Still Romantically Involved?
Best Far Side Jokes
The Best Transmission Cooler [Buying Guide]
Lions Roster Wiki
Ups Locations Massachusetts
Doomz.io Unblocked Games 76
Merging Rooms Fallout Shelter
Filmy4Wab Xyz
Spartan 365 - Email and Microsoft Office
Portugal Anúncios Classificados OLX
1977 Elo Hit Wsj Crossword
Lids Locker Room Vacaville Photos
Eggy Car - Play it Online at Coolmath Games
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.