Python Tutorial - Master Python Programming For Beginners from Scratch

  • Home
  • Getting Started
  • Basics
  • OOP
  • Advanced Python
  • Tkinter
Home » Python Basics » Python Type Conversion

Python Type Conversion

Summary: in this tutorial, you’ll learn about Python a type conversion in Python.

Introduction to type conversion in Python

To get an input from users, you use the input() function. For example:

value = input('Enter a value:') print(value)
Code language: Python (python)

When you execute this code, it’ll prompt you for an input:

Enter a value:
Code language: Python (python)

If you enter a value, for example, a number, the program will display that value back:

Enter a value:100 100
Code language: Python (python)

The input() function, however, returns a string.

The following example prompts you for two input values: net price and tax rate. After that, it calculates the net price and displays the result on the screen:

price = input('Enter the price ($):') tax = input('Enter the tax rate (%):') net_price = price * tax / 100 print(f'The net price is ${net_price}')
Code language: Python (python)

When you execute the program and enter some numbers:

Enter the price ($):100 Enter the tax rate (%):10
Code language: Python (python)

… you’ll get the following error:

Traceback (most recent call last): File "app.py", line 4, in <module> net_price = price * tax / 100 TypeError: can't multiply sequence by non-int of type 'str'
Code language: Python (python)

Since the input values are strings, you cannot apply the mathematical operators to them.

To solve this issue, you need to convert the strings to numbers before performing calculations.

To convert a string to a number, you use the int(str) function. More precisely, the int(str) function converts a string to an integer.

The following example uses the int(str) to convert the input strings to numbers:

price = input('Enter the price ($):') tax = input('Enter the tax rate (%):') net_price = int(price) * int(tax) / 100 print(f'The net price is ${net_price}')
Code language: Python (python)

If you run the program, enter some values, you’ll see that it works correctly:

Enter the price ($):100 Enter the tax rate (%):10 The net price is $ 10.0
Code language: Python (python)

Other type conversion functions

Python has many type conversion functions besides the int(str) functions. Here are the most important ones to you for now:

  • float(str) – convert a string to a floating-point number.
  • bool(val) – convert a value to a boolean value, either True or False.
  • str(val) – return the string version of a value.

Getting the type of a value

To get the type of a value, you use the type(value) function. For example:

>>> type(100) <class 'int'> >>> type(2.0) <class 'float'> >>> type('Hello') <class 'str'> >>> type(True) <class 'bool'>
Code language: Python (python)

As you can see clearly the output:

  • The number 100 has the type of int.
  • The number 2.0 has the type of float.
  • The string 'Hello' has the type of str.
  • And the True value has the type of bool.

In front of each type, you see the class keyword. It isn’t important for now. And you’ll learn more about it later.

Summary

  • Use the input() function to get an input string from users.
  • Use type conversion function such as int(str), float(str), bool(value), and str(value) to convert a value from one type to another.
  • Use the type() function to get the type of a value.
  • Was this tutorial helpful ?
  • YesNo
Previous Python Comments
Next Python Comparison Operators

Getting Started

  • What is Python
  • Install Python
  • Setup VS Code for Python
  • Develop Python Hello World Program

Python Fundamentals

  • Python Syntax
  • Variables
  • Strings
  • Numbers
  • Boolean
  • Constants
  • Comments
  • Type Conversion

Control Flow

  • if…else
  • Ternary Operator
  • for Loop
  • while Loop
  • break
  • continue
  • pass

Functions

  • Python Functions
  • Default Parameters
  • Keyword Arguments
  • Recursive Functions
  • Lambda Expressions
  • Function Docstrings

Python List

  • List
  • Tuple
  • Sorting a List in Place: sort()
  • Sorting a List: sorted()
  • Slicing a List: [::]
  • Unpacking a List
  • Iterating over a List: for loop
  • Finding Index of an Element: index()
  • Iterables
  • Transform List Elements: map()
  • Filtering List Elements: filter()
  • Reducing List Elements: reduce()
  • List Comprehensions

Python Dictionary

  • Dictionary
  • Dictionary Comprehension

Python Set

  • Set
  • Set Comprehension
  • Union of Sets
  • Intersection of Sets
  • Difference between Sets
  • Symmetric Difference of Sets
  • Subset
  • Superset
  • Disjoint Sets

Exception Handling

  • try…except
  • try…except…finally
  • try…except…else

Python Loop with Else Clause

  • for…else
  • while…else
  • do…while Emulation

More on Functions

  • Unpacking Tuples
  • The *args Parameters
  • The **kwargs Parameters
  • Partial Functions

Modules

  • Modules
  • Module Search Path
  • Python __name__
  • Packages

File I/O

  • Reading a Text File
  • Writing to a Text File
  • Creating a Text File
  • Checking If a File Exists
  • Reading from a CSV File
  • Writing to a CSV File

Managing Third-party Packages

  • Python Package Index (PyPI) & PIP
  • Python Virtual Environments
  • Install pipenv on Windows
  • Using pipenv Tool

About pythontutorial.net

Pythontutorial.net helps you master Python programming from scratch fast.

Site Links

  • Home
  • Contact
  • About
  • Privacy Policy

Recent Python Tutorials

  • Tkinter StringVar
  • Tkinter PhotoImage
  • Tkinter OptionMenu
  • Tkinter ScrolledText
  • How to Change the Appearances of Widgets Dynamically Using Ttk Style map() Method

Python References

  • Python String Methods
  • Python MySQL
  • Python PostgreSQL
  • Python Oracle
  • Python SQLite

Copyright © 2021 · by pythontutorial.net. All rights reserved. Log in