Python Type Conversion (original) (raw)

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)

Try it

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)

Note that the Playground will display an input dialog instead of a prompt.

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)

Try it

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)

Try it

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:

Getting the type of a value #

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

`result_type = type(100) print(result_type)

result_type = type(2.0) print(result_type)

result_type = type('Hello') print(result_type)

result_type = type(True) print(result_type)`Code language: Python (python)

Try it

Output:

<class 'int'> <class 'float'> <class 'str'> <class 'bool'>Code language: HTML, XML (xml)

In the output:

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 #

Quiz #

Was this tutorial helpful ?