Type Conversion in Python

How to make Type Conversion in Python

What is type conversion, why is it useful, and how to use type conversion in Python?

Step 1: What is type conversion in Python?

As we learned, variables in Python have types.

Sometimes we need to convert from one type to another. Say, you have an integer variable, but you need the value represented as a string.

That is a type conversion. Converting from one type to another.

Notice that type conversion has many names, you might have heard of typecasting, type coercion, or type juggling. But they are all about changing the expression of one data type to another.

Step 2: Why do we need type conversion?

Let’s demonstrate an issue.

Consider the following program.

name = input("What is your name? ")
print(f"Hello {name}!")
birth_year = input("What is your birht year? ")
print(f"You are {2021 - birth_year} old!")

Well, you expect the program to write out your age.

Unfortunately, it will not function.

Why? Because 2021 – birth_year is not valid. 2021 is an integer, while birth_year is a string. But, you cannot subtract a string from an integer.

So what to do?

Type conversion.

Step 3: How to make type conversion in Python

Now we understand the problem. Let’s try to solve it.

Luckily, Python is our friend and has built-in functions.

  • int() Converts to an integer.
  • float() Converts to a float.
  • str() Converts to a string.

Let’s try the first one.

name = input("What is your name? ")
print(f"Hello {name}!")
birth_year = input("What is your birht year? ")
birth_year = int(birth_year)
print(f"You are {2021 - birth_year} old!")

Now it works as expected and the only difference is the statement birth_year = int(birth_year).

Nice and easy, it converts birth_year to an integer (if possible)

The other functions work similarly.

What next?

I am happy you asked.

If this is something you like and you want to get started with Python, then this is part of an 8 hours FREE video course with full explanations, projects on each level, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • 2 FREE eBooks to support your Python learning.

See the full FREE course page here.

Learn Python

Learn Python A BEGINNERS GUIDE TO PYTHON

  • 70 pages to get you started on your journey to master Python.
  • How to install your setup with Anaconda.
  • Written description and introduction to all concepts.
  • Jupyter Notebooks prepared for 17 projects.

Python 101: A CRASH COURSE

  1. How to get started with this 8 hours Python 101: A CRASH COURSE.
  2. Best practices for learning Python.
  3. How to download the material to follow along and create projects.
  4. A chapter for each lesson with a descriptioncode snippets for easy reference, and links to a lesson video.

Expert Data Science Blueprint

Expert Data Science Blueprint

  • Master the Data Science Workflow for actionable data insights.
  • How to download the material to follow along and create projects.
  • A chapter to each lesson with a Description, Learning Objective, and link to the lesson video.

Machine Learning

Machine Learning – The Simple Path to Mastery

  • How to get started with Machine Learning.
  • How to download the material to follow along and make the projects.
  • One chapter for each lesson with a Description, Learning Objectives, and link to the lesson video.

Leave a Comment