Python Type Conversion

Summary: in this tutorial, you’ll learn about type conversion in Python and some useful type conversion functions.

Introduction to type conversion in Python

To get 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 input on the Terminal:

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
100Code language: Python (python)

However, the input() function returns a string, not an integer.

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

price = input('Enter the price ($):')
tax = input('Enter the tax rate (%):')

tax_amount = price * tax / 100

print(f'The tax amount price is ${tax_amount}')Code language: Python (python)

When you execute the program and enter some numbers:

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

… you’ll get the following error:

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    tax_amount = 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 multiply operator.

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() function. More precisely, the int() function converts a string to an integer.

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

price = input('Enter the price ($):')
tax = input('Enter the tax rate (%):')

tax_amount = int(price) * int(tax) / 100
print(f'The tax amount is ${tax_amount}')Code language: Python (python)

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


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

Other type conversion functions

Besides the int(str) functions, Python supports other type conversion functions. The following shows the most important ones 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 representation of a value.

Getting the type of a value

To get the type of 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 from 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 the class later.

Summary

  • Use the input() function to get an input string from users.
  • Use type conversion functions such as int(), float(), bool(), and str(vaue)to convert a value from one type to another.
  • Use the type() function to get the type of a value.
Did you find this tutorial helpful ?