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:
Code language: Python (python)Enter a value:
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, eitherTrue
orFalse
.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 ofint
. - The number
2.0
has the type offloat
. - The string
'Hello'
has the type ofstr
. - And the
True
value has the type ofbool
.
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)
, andstr(value)
to convert a value from one type to another. - Use the
type()
function to get the type of a value.