Python – Read Number from Console

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

  1. Call input() function and prompt user to enter a number.
input('Enter a number: ')
  1. input() function returns a string. Convert this string to integer using int() built-in function.
int(input('Enter a number: '))
  1. 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: '))
  1. 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))
Copy

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)
Copy

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.

  1. Call input() function and prompt user to enter a floating point number.
  2. input() function returns a string. Convert this string to floating point number using float() built-in function.
  3. float() function returns a floating point number created from the user entered value. Assign the returned value to a variable, say n.
  4. 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))
Copy

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.

Frequently Asked Questions

1. How do you read an integer using input() function in Python?

Answer:

You can read in integer from user using input() and int() functions. input() function returns the value entered by the user in the prompt as a string. Then, you can pass the entered value as argument to the int() function, which converts the given string into an integer and returns the integer value.

age = int(input('Enter your age : '))
2. How do you read a float value in Python?

Answer:

You can read a floating point value from user using input() and float() functions. input() function returns the value entered by the user in the prompt as a string. Then, you can pass the entered value as argument to the float() function, which converts the given string into a float value and returns the float value.

height = float(input('Enter your height in meters : '))

Related Tutorials

Code copied to clipboard successfully 👍