Learn how you can become a Python programmer in just 12 weeks.

    We respect your privacy. Unsubscribe at anytime.

    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?

    In this tutorial, you will:

    • Understand Type Conversion: Learn about type conversion, the process of changing the data type of a value from one form to another in Python.
    • Explore the Benefits of Type Conversion: Discover why type conversion is useful, allowing you to perform operations, comparisons, and manipulations on different data types.
    • Master Type Conversion in Python: Learn how to use built-in functions and techniques in Python to perform type conversion effectively.

    By the end of the tutorial, you will have a solid understanding of type conversion in Python and its significance. You will be equipped with the knowledge and skills to convert data types, enabling you to work with diverse data and perform operations seamlessly.

    Watch tutorial

    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?

    In the next lesson you will learn about program flows in Python.

    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.
    • A FREE eBook to support your Python learning.

    See the full FREE course page here.

    Python for Finance: Unlock Financial Freedom and Build Your Dream Life

    Discover the key to financial freedom and secure your dream life with Python for Finance!

    Say goodbye to financial anxiety and embrace a future filled with confidence and success. If you’re tired of struggling to pay bills and longing for a life of leisure, it’s time to take action.

    Imagine breaking free from that dead-end job and opening doors to endless opportunities. With Python for Finance, you can acquire the invaluable skill of financial analysis that will revolutionize your life.

    Make informed investment decisions, unlock the secrets of business financial performance, and maximize your money like never before. Gain the knowledge sought after by companies worldwide and become an indispensable asset in today’s competitive market.

    Don’t let your dreams slip away. Master Python for Finance and pave your way to a profitable and fulfilling career. Start building the future you deserve today!

    Python for Finance a 21 hours course that teaches investing with Python.

    Learn pandas, NumPy, Matplotlib for Financial Analysis & learn how to Automate Value Investing.

    “Excellent course for anyone trying to learn coding and investing.” – Lorenzo B.

    Leave a Comment