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
3 Rules For Using Margin Safely And Profitably
API key authentication - C# tutorial - NHS Digital
3 Tick Granite Osrs
Time in Baltimore, Maryland, United States now
Sound Of Freedom Showtimes Near Governor's Crossing Stadium 14
855-392-7812
Pangphip Application
Myexperience Login Northwell
Jennette Mccurdy And Joe Tmz Photos
Athletic Squad With Poles Crossword
Ogeechee Tech Blackboard
Ave Bradley, Global SVP of design and creative director at Kimpton Hotels & Restaurants | Hospitality Interiors
2021 Lexus IS for sale - Richardson, TX - craigslist
Whitley County Ky Mugshots Busted
5808 W 110Th St Overland Park Ks 66211 Directions
Oc Craiglsit
Nioh 2: Divine Gear [Hands-on Experience]
4156303136
سریال رویای شیرین جوانی قسمت 338
Justified Official Series Trailer
Water Trends Inferno Pool Cleaner
Where Is George The Pet Collector
2024 INFINITI Q50 Specs, Trims, Dimensions & Prices
Aes Salt Lake City Showdown
Wics News Springfield Il
eugene bicycles - craigslist
Обзор Joxi: Что это такое? Отзывы, аналоги, сайт и инструкции | APS
Soul Eater Resonance Wavelength Tier List
Darrell Waltrip Off Road Center
2015 Kia Soul Serpentine Belt Diagram
Roseann Marie Messina · 15800 Detroit Ave, Suite D, Lakewood, OH 44107-3748 · Lay Midwife
Ehome America Coupon Code
Craigslist Texas Killeen
Craigslist Maryland Baltimore
Nacho Libre Baptized Gif
Louisville Volleyball Team Leaks
Are you ready for some football? Zag Alum Justin Lange Forges Career in NFL
3400 Grams In Pounds
968 woorden beginnen met kruis
Davis Fire Friday live updates: Community meeting set for 7 p.m. with Lombardo
Clima De 10 Días Para 60120
Simnet Jwu
Ezpawn Online Payment
Subdomain Finder
Shipping Container Storage Containers 40'HCs - general for sale - by dealer - craigslist
3 bis 4 Saison-Schlafsack - hier online kaufen bei Outwell
6576771660
2017 Ford F550 Rear Axle Nut Torque Spec
8 4 Study Guide And Intervention Trigonometry
Windy Bee Favor
Lesson 5 Homework 4.5 Answer Key
Black Adam Showtimes Near Cinemark Texarkana 14
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.