codescracker


python

Python Program to Get Input from User



« Previous Program Next Program »


Get Input from User in Python

To get input from user in python, you have to use input() function. You can get any type of input using this input() function in python, you have only to place the type before the statement to get the desired type of input using the input() statement in python. Let's first start with discussing how to get integer input from user in python.

Python Programming Code to Get Input from User

Following are some python programs shows how to get integer, floating-point, and string input from user in python.

Get Integer Input from User

This python program illustrates how to get integer input from user in python:

# Python Program - Get Integer Input from User
while True:
	print("Enter '0' for exit.")
	val = int(input("Enter any number: "))
	if val == 0:
		break
	else:
		print("You have just entered:", val)
	print()

Here is the sample run of the above Python program:

python get integer input from user

Get Floating-point Input from User

This python program shows how to get floating-point input from user in python:

# Python Program - Get Floating-point Input from User
while True:
	print("Enter '0' for exit.")
	val = float(input("Enter any number: "))
	if val == 0:
		break
	else:
		print("You have just entered:", val)
	print()

Here is the sample run of the above Python program:

python get floating-point input from user

Get String Input from User

Here is another program shows how to get string input from user in python:

# Python Program - Get String Input from User
while True:
	print("Enter 'x' for exit.")
	val = input("Enter any string: ")
	if val == 'x':
		break
	else:
		print("You have just entered:", val)
	print()

Here is the sample run of the above Python program:

python get string character input from user

Complete Version of Getting Input from User

Here is the complete version of getting input from user in python:

# Python Program - Get Input from User - Complete Version
while True:
	print("Enter '0' for exit.")
	val = int(input("Enter any Number: "))
	flt = float(input("Enter any Floating-point Number: "))
	strg = input("Enter any String: ")
	if val == 0:
		break
	else:
		print("Integer:",val)
		print("Floating-point:",flt)
		print("String:",strg)
	print()

Here is the sample run of the above Python program:

python get input from user

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:


« Previous Program Next Program »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test