Python Program: Number/Integer Input

In this tutorial, you will learn how to take an integer input in Python 3.

Python has the built-in function called input() which prints the passed argument as a prompt into the screen, without a trailing newline. It then reads the input typed-in by the user and returns it as a converted string.

				
				name = input("What is your name?")
				print("Your name is", name)
				print()
				
			
python program name input

If you require only integer inputs, you need to type-cast the input into an integer. For this, we make use of the built-in function int() which returns an integer object to the passed argument. So, int('12') will return the value 12. If you give a float value int('12.5'), it will also return 12.

Here is the program in Python that reads an integer input and displays it.

				
				n = int(input("Enter a number: "))
				print("The number you have entered is", n)
				print()
				
			

On executing the above program, it prompts for an input number and then prints it.

python program integer input

Notes

  • Type-casting the input into float and string works the same way using float() as well as str(). You can do float(input(":")) or str(input(":")).