Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Type Conversion & Type Casting in Python Explained with Examples

Updated on 19 November, 2024

10.71K+ views

Python has many data types, and as a programmer, you should know how to convert one data type of Python into another. Python has many built-in functions for this task. And in this simple guide, we’ll be taking a look at them to help you understand type conversions better.

After you’re through with this guide, you would know how to convert strings into integers and vice versa. So without further ado, let’s get started. 

Data Types in Python

Before we begin our discussion on type conversion in Python, let’s take a brief look at all data types present in this language:

Integers

Integer values can be as long as you want. Like in mathematics, integers in Python are digits too. But you can’t have indefinitely long integers unless you have a supercomputer. Python treats any number without a prefix as an integer. These numbers include decimal digits too.

Floating Point Numbers

Floats in Python are numbers that are written with a decimal point for separating the fractional digits and the integer. Some examples of floating-point numbers are 4.3, 9.18, etc. You can add ‘e’ for the scientific notation in Python as well. 

Complex Numbers

Complex numbers in Python follow this format: (the real part + the imaginary part). Some examples of complex numbers are (3+2i), (8-2x), etc. 

Strings

Strings can be any particular set of characters in Python. They are referred to as str. You can write them by using quotation marks. You can use single quotes as well as double quotes for writing strings in Python. Strings can be any characters placed in quotation marks such as ’91’, “hello”, etc. 

Just like integers, there is no limit to character length in strings. Strings are of various types, such as triple-quoted strings, raw strings, and many others. However, if we would start discussing strings and their kinds, this article will go too long. 

Boolean Data Type

The final data type we have left to discuss is the Boolean data type. Boolean data can have one of two values, which are true and false. Boolean values are quite essential in Python for many reasons. You can use Boolean values to determine the truthiness of objects and values. 

Read: Python Developer Salary in India 2020

As you’ll learn more about logical operators in Python, you will have to deal with Boolean objects and values. 

So these were all data types present in Python. Learn more about the data types in Python. Apart from these data types, Python also has many built-in functions, which help it in performing a variety of tasks. We’ll need to use a few built-in functions to implement type conversion as well. Checkout our data science programs to learn about various types of conversions.

Now that we’ve discussed data types, we can move onto type conversion. 

upGrad’s Exclusive Data Science Webinar for you –

How upGrad helps for your Data Science Career?

Different Kinds of Type Conversions in Python

There are two kinds of type conversions in Python. They are the following:

Implicit

In this case, Python converts a data type into another type automatically. You, the user, don’t have to get involved in this process. Here is an example of implicit conversion of integer to float in Python:

num_int = 18

num_flo = 1.8

num_new = num_int + num_flo

print(“datatype of num_int:”,type(num_int))

print(“datatype of num_flo:”,type(num_flo))

print(“Value of num_new:”,num_new)

print(“datatype of num_new:”,type(num_new))

The output of the above code would be the following:

datatype of num_int: <class ‘int’>

datatype of num_flo: <class ‘float’>

Value of num_new: 19.8

datatype of num_new: <class ‘float’>

As you can see, we only had to add num_flo and num_int along with num_new to get the desired output. We converted the integer 18 into a float 19.8 through a few lines of code. The vital thing to note here is that Python turned the integer into float automatically.

Explicit

In the case of explicit type conversion, we use functions for converting purposes. Remember the built-in functions we mentioned before? They will come in handy in this section. Another name for explicit type conversions is typecasting. It has this name because you, the user, cast the data type of the concerned object. 

There are some type conversion functions you should be familiar with to perform typecasting in Python. Int(a,base) is a popular function as it can convert any data type into an integer. float() can turn any data type into a floating-point number. oct() converts integers to octal strings, and hex() converts integers to hexadecimal strings. 

Here is an example of type conversion using oct():

c = oct(80)

print (“Output : “,end=””)

print (c)

And the output would be:

Output : 0o120

When working with these functions, remember that the syntax for such conversion should be:

(desired_datatype)(expression)

There are many kinds of type conversions you can perform in Python. However, there are two kinds of them, which you’ll be performing the most. Converting strings into integers and vice versa can be a great starting point. So let’s discuss each of those type conversions separately for a better understanding of this topic:

Read: Python Developer Project Ideas & Topics

Converting String to int Python 

You can convert a string to int Python through the int() function. It gives you a decimal integer with just a click. It assumes that the expression you’ve entered is a decimal integer, but if it’s not, it will return a ValueError. And we don’t want that. You can mention the number system you require by giving the value a base to avoid this problem. 

So, if you’re converting a string to int in Python you can do this:

int(“5”)

Or, you can add a base for different kinds of strings:

int(“5” , base=10)

Now let’s discuss the reverse of this conversion, i.e. when you need to convert an integer into a string.

Converting int to String Python

You can use the str() function for converting integers into strings. Just follow the syntax we mentioned before:

str(5)

Strings can convert integers into decimal expressions if you use a binary literal. However, just like with the int function, if you require, you can add more information for different conversions. You can get octal, binary, and even hexadecimal results with this function. 

After a little practice, you can perform more complex tasks with type conversion as well. Here’s an example of adding a string with an integer through typecasting:

num_int = 256

num_str = “64”

print(“Data type of num_int:”,type(num_int))

print(“Data type of num_str before Type Casting:”,type(num_str))

num_str = int(num_str)

print(“Data type of num_str after Type Casting:”,type(num_str))

num_sum = num_int + num_str

print(“Sum of num_int and num_str:”,num_sum)

print(“Data type of the sum:”,type(num_sum))

The output of the above code would be this:

Data type of num_int: <class ‘int’>

Data type of num_str before Type Casting: <class ‘str’>

Data type of num_str after Type Casting: <class ‘int’>

Sum of num_int and num_str: 320

Data type of the sum: <class ‘int’>

As you can see, it’s a lot of fun. Feel free to experiment with type conversions and expand your knowledge base. 

Conclusion

We hope you learned a lot from this article. Performing type conversions is one of the many things you can do with Python’s functions. 

If you are reading this article, most likely you have ambitions towards becoming a Python developer. If you’re interested to learn python & want to get your hands dirty on various tools and libraries, check out IIIT-B’s PG Diploma in Data Science.

Frequently Asked Questions (FAQs)

1. In coding, how are explicit conversions different from implicit conversions?

An implicit type conversion is a type conversion that is defined by the user and compels an expression to be of a certain type. When several data types are combined together in an expression, the compiler performs an implicit type conversion. Explicit conversion, also known as casting, is when we explicitly convert from one data type to another by executing the appropriate function for the data type we wish to convert to. Without the assistance of the programmer, an implicit type conversion is accomplished.

2. How is type casting different from type conversion?

The destination data type must be smaller than the source data type when type casting one data type to another. However, the source data needs to be smaller than the destination data type when converting one data type to another. A programmer must manually type cast one kind of data into another type. Converting one data type to another does not involve any programmer intervention because the compiler automatically compiles it during program execution. There is a chance that data or information will be lost during type casting. When converting a small data type to a large data type, data is unlikely to be lost.

3. Which is more efficient in Python- tuples or lists?

A tuple is a comma-separated collection of Python objects and because of its static nature, the tuple is quicker than the list. Tuples are also more efficient in terms of memory than lists. When it comes to time efficiency, tuples have a minor edge over lists, particularly when it comes to looking up to a value. If you have data that isn't supposed to be altered in the first place, a tuple data type should be preferred over a list. We may make a list of tuples, which means that the members of the tuple can be wrapped in a list and therefore follow the same characteristics as a Python list. Because Python Tuples take up less space, constructing a list of tuples is more practical.