Read Number from Console in Python
Read Number from Console Input in Python
To read a number from console in Python input by user, you can use input() function.
input() functions enables your Console Python application to read input from user.
Usually input() function reads a string value from console. Also, Python3 does not differentiate if the input is a string or number. Whatever the user provides as input through the console, it takes in as a String. Once we get the string input from user via console, we can convert this string to an integer of a float value.
Examples
1. Read integer from console in Python
In this example, we have to read an integer number from user using input() function in Python.
Steps
- Call input() function and prompt user to enter a number.
input('Enter a number: ')
- input() function returns a string. Convert this string to integer using int() built-in function.
int(input('Enter a number: '))
- int() function returns an integer value created from the user entered value. Assign the returned value to a variable, say n.
n = int(input('Enter a number: '))
- You may print this number to output, and also print the type of value in variable n using type() built-in function.
Python Program
# Read an integer from user
n = int(input('Enter a number: '))
# Print integer to output
print('You entered ', n)
# Print type of value in n to output
print(f'{n} is of type', type(n))
Output
Enter a number: 52
You entered 52
52 is of type <class 'int'>
The <class 'int'>
represents that the variable n1
is of class type int
, short for integer.
You can also read multiple integer values from user, and perform arithmetic operations. For example, in the following program, we read two numbers from user via console input, find their sum, and print the sum to output.
Python Program
# Read integers from user
n1 = int(input('Enter a number : '))
n2 = int(input('Enter another number : '))
sum = n1 + n2
print(f'Sum of {n1}, {n2} is', sum)
Output
Run the program and you shall see a prompt for reading n1
. Enter a number and click enter key. Then you shall see the prompt for reading n2
. Enter a number and click enter key. Once the two numbers are read, they are added using Addition operator, and the result is print to console output.
Enter a number : 52
Enter another number : 14
Sum of 52, 14 is 66
Arithmetic Operation has been performed, because n1 and n2 are integers, and not strings.
2. Read float from console in Python
In the following program, we have to read a float value from user via console input.
CA input() function. input() function returns a string, and this string should be passed as argument to the float() function. The float() function returns a floating point value created from the user entered input.
- Call input() function and prompt user to enter a floating point number.
- input() function returns a string. Convert this string to floating point number using float() built-in function.
- float() function returns a floating point number created from the user entered value. Assign the returned value to a variable, say n.
- You may print this number to output, and also print the type of value in variable n using type() built-in function.
Python Program
# Read a float from user
n = float(input('Enter a float number: '))
# Print float to output
print('You entered', n)
# Print type of value in n to output
print(f'{n} is of type', type(n))
Output
Enter a float number: 3.14
You entered 3.14
3.14 is of type <class 'float'>
Summary
In this tutorial of Python Examples, we learned how to read an integer from console: read string using input() and then typecast it using int(); and then how to read a floating point value using input() and float() built-in functions; with the help of well detailed Python example programs.