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

print("Enter '0' for exit.");
val = int(input("Enter any number: "));
if val == 0:
    exit();
else:
    print("You have just entered:",val);

Here is the sample run of the above Python program:

This is the initial output.

receive input from user python

Enter any number (integer number) say 10 and press enter to see the following output.

get input from user python

Below is the screenshot of the python shell where the same program is written and run directly.

python get integer input from user

Program Video with Compile and Run

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

print("Enter '0' for exit.");
val = float(input("Enter any floating-point number: "));
if val == 0:
    exit();
else:
    print("You have just entered:", val);

Here is the sample run of the above Python program:

take input from user python

Now type any floating-point value say 10.48 and press enter to see the below output.

take user input python

Below is the same program with run directly on python shell.

python get floating-point input from user

Program Video with Compile and Run

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

print("Enter 'x' for exit.");
val = input("Enter any string value: ");
if val == 'x':
    exit();
else:
    print("String entered by you:", val);

Here is the sample run of the above Python program:

get string input python

Now type any string value say codescracker and press enter to see the below output.

get string user input python

Below is the same program with run directly on python shell.

python get string character input from user

Program Video with Compile and Run

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

print("Enter '0' for exit.");
val = int(input("Enter any integer number: "));
flt = float(input("Enter any floating-point number: "));
strg = input("Enter any string: ");
if val == 0:
    exit();
else:
    print("\nHere are the values entered by you:");
    print("Integer:",val);
    print("Floating-point:",flt);
    print("String:",strg);

Here is the sample run of the above Python program:

This is initial output, asking user to first enter any integer value.

getting user input python

Now type any integer value, then program ask to enter any floating-point value, and then any string value, enter all the three values say 10, 10.48 and codescracker and press enter to see the output given in the below screenshot:

user input taking program python

Below is the same program written and run directly on python shell.

python get input from user

Program Video with Compile and Run

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