Python user input Examples | Python input() function

Related Searches: python user input, python get user input, python get input, python read input, python ask for input, user input, how to get user input in python, take input in python, take input from user in python, how to get input in python, python take input, python ask for user input, how to take user input in python, get input from user python, python ask, w3schools input, how to ask for input in python, python read user input, python ask user for input, python how to get user input, how to ask user for input in python, how to get input from user in python, getting user input in python, python to take input from user, how to take a input in python 

 

Introduction to Python user input method

While programming, we often come across a situation where we need to take input from the user to manipulate the data and process it.  For this reason, most of the programming languages provide a user input functionality, which takes inputs from the user. In python, we have a python user input method, which helps us to take input from the user and process the data. In this tutorial, we will learn about the python user method in depth. We will take different examples of the python input method and will discuss how we can take different data types of input as well. In a nutshell, this tutorial will cover everything that you need to know about the python user input method.

Advertisement

 

Getting started with the python user input method

Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programming languages have a user input method to ask the user to provide some sort of data. In the same way, python provides us user input method which helps us to take input from the user. In this section we will discuss the syntax of the python user input method along with some examples.

 

Python user input method syntax with examples

Python has a user input function that helps us to take input from the user. When we use the input method in our program, then the flow of execution is halted till the user inputs the data and clinks the enter button. Once user inputs data and clicks on enter button, the input value is then stored in some variable in the form of a string. Here is simple syntax of python user input method.

variable_name = input()

Not let us take a simple program and which takes the input from the user. See the following python code.

# python user input() function
name = input()

Once we run this program, the program will ask the user to input any data, and it will not execute further unless the user enters data. See the example below:

Python user input Examples | Python input() function

Now the program will wait for the user to input some sort of data and when the user enters data and press enter button, then the program will finish its execution.

 

Give a helpful hint during the prompt

In the above example, notice the there is nothing on the terminal, only the blinking of the cursor, which asks the users to input data. We can also make our program more beautiful and readable by showing some message to the user, maybe about the kind of data we expect from the user. In this section, we will see how we can give a helpful hint to the user during the prompt.  The simple syntax of the python user input method with useful message looks like this:

Advertisement
variable_name = input("the message to be shown")

Now let us take our previous example, but this time we will show some messages as well. See the example below:

# python user input method
name = input("Please enter your name! ")

This time we get the following output:

Python user input Examples | Python input() function

Notice that this time we get the message along with a prompt that says to enter the name of the user.

 

Python print() function to ask for user input

Now we have enough knowledge of how we can get input from users. Let us now see how we can print the same input that we take from the user back to the user. First, we need to take input from the user, then store the data in some variable and print out that variable. See the example below

# python ask for user input and store in name variable
name = input("Please enter your name! ")

# printing the input
print("our user name is {}".format(name))

Output:

Please enter your name! Bashir
our user name is Bashir

Now let us take one more example, which takes data about a student and then prints out the data. See the example below:

# python ask for user input and store in student_name variable
student_name = input("Please enter your name! ")

# get user input and collect subjects taken
subject1 = input("Enter the subject name! ")
subject2 = input("Enter the second subject name! ")

# printing th collected data
print("Congratulations!! {} you have successfully enrolled to subjects; {} and {}".format(student_name, subject1, subject2))

Output:

Please enter your name! Erlan
Enter the subject name! math
Enter the second subject name! Computer Science
Congratulations!! Erlan you have successfully enrolled to subjects; math and Computer Science

 

Python user input types

Everything that we take from the user as input is considered to be string type in python. We can check the data type using python to confirm the variable data type. See the example below:

Advertisement
# python ask for user input and store in student_name variable
student_name = input("Please enter your name! ")

# checking data type
print("the data type of variable is {}.".format(type(student_name)))

Output:

Please enter your name! bashir
the data type of variable is <class 'str'>.

Even if we enter an integer or floating value, still it will be treated as a string. See the example below:

# python ask for user input and store in student_age variable
student_age = input("please enter your age: ")

# checking data type
print("the data type of variable is {}.".format(type(student_age)))

Output:

please enter your age: 12
the data type of variable is <class 'str'>.

Notice that even we provide integer type but still the python user input method treats it as an integer type and we will get an error if we try to add it with integers. See the following example.

# python user input method
student_age = input("please enter your age: ")

# adding different data type
print(23 + student_age)

Output:

Python user input Examples | Python input() function

Notice that we get TypeError which is because we cannot add strings with int type.

Now in the following section, we will see how we can add user input data and different data types together.

 

Example of python user input as integer

If we want to take an integer input from the user, we have to explicitly mention the input type to be an integer. See the following syntax for python int data type input.

Advertisement
variable_name = int(input())

Now let us take an example and see if we really get an integer value or not. See the example below:

# Store python user input as integer
student_age = int(input("please enter your age: "))

# checking data type
print("the data type is {}" .format(type(student_age)))

Output:

please enter your age: 12
the data type is <class 'int'>

Notice that the variable type is now int because we had explicitly changed it to the integer type. Now if we add the python user input and an integer, we will not get an error. See the example below:

# Store python user input as integer
student_age = int(input("please enter your age: "))

# adding
print(12+student_age)

Output:

please enter your age: 13
25

 

Example of python user input as float type

In a similar way we can, take user input as float type by explicitly mentioning the data type to be float. The syntax looks like this:

variable_name = float(input())

Now let us take an example to confirm that our input data type is float, not a string which is by default. See the example below:

# Store python user input as float
student_age = float(input("please enter your age: "))

# checking data type
print("data type is {}".format(type(student_age)))

Output:

please enter your age: 12
data type is <class 'float'>

Notice that this time we get float type because we explicitly mentioned the type to be float. Now let us take an example of float adding and see if we can add floating numbers with another number. See the example below:

# Store python user input as float
student_age = float(input("please enter your age: "))

# adding
print(12+student_age)

Output:

please enter your age: 12
24.0

Notice that we had got the floating number as result.

 

Collect multiple inputs using the python user input method

So far we have learned how we can take input from users using the python user input method. If we want to take multiple inputs from the user, one method be to use the python user input method multiple times. For example like this one:

# python user input method
student_age = int(input("please enter your age: "))
student_grade = input("enter grade")
student_class = input("class name")

This could be one way but it takes too many lines and repeats the same code if we have to take a large number of inputs from the user. However, there is a more pythonic way of taking multiple inputs from users in one line. See the example below:

# python user input method
student_name, studet_class, student_grade = input("Enter your Name, class, grade separated by space ").split()

print("printing the input data......")
print("name :{}\n class: {}\n student grade: {}".format(studet_class, studet_class, student_grade))

Output:

name :12
class: 12
student grade: A

The split() method gets the value of individual input. One of the drawbacks of this method is that we cannot take different data types in using this method.

 

Summary

Sometimes we might come across a situation, where we take input from users and process the data. In such a situation python user input method help us to take data from the user. In this tutorial, we learned how we can take input from users taking different examples. We came to know that by default, the input data type in Python is a string, so we learned how we can take float and int data type values from users. Moreover, we also learned how we can take multiple inputs from users in one line. All in all, this tutorial covers everything that you need to learn to get started with the python user input method.

 

Further Reading

Python user input method
more about python user input
Built-in methods

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment