Type Conversion in Python

What will we cover?

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 type conversion. Converting from one type to another.

Notice that type conversion has many names, you might have heard of type casting, type coercion, or type juggling. But they are all about changing the expression of a 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 a 8 hours FREE video course with full explanations, projects on each levels, 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.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Python Like a Pro?

If you’re serious about learning Python, there’s nothing better than strong commits. At your request, we have created an improved version of this popular free online course.

This version has the following benefits to enhance your learning journey.

  1.  Tracking your progress in the course.
  2.  Questionaries to ensure you understand concepts between important lessons.
  3. Downloadable Cheat Sheets for fast lookup what you learned.
  4. Direct Q&A with the instructor to help you to understand the material better.
  5. Added material for better explanations and insider knowledge.
  6. Extra videos with more explanations and stories.
  7.  Certificate at completion.

Start the change in your life and commit to doing something amazing that you have always dreamed of.

Sign up and become part of the exclusive Python Like a Pro elite.

Leave a Reply

%d bloggers like this: