In python programming, sometimes we need inputs from the users. In other words, we ask something to the user of the program and wait some characters from him/her. Then, we use these inputs in the other parts of the program. In this lesson, we will focus on this Python user input. We will learn, how to get an input from a user with python programming.
To get an input from the user, we use input() method. This is true for higher versions of Python. If you are using Python 2.7 or lower, this method is raw_input(). But, here we will use the new version of this method.
So, why we need any input from the user? Basically, we can use this function in many areas and for many questions. We can ask a username, a password basically. We can ask age, sex, profession, address etc. We can ask for a character, for a number, for anything that we can write with keyboard. This is limited with your imagination and with your program.
Let’s given an example for input method in python and learn it better. We can start by asking a username ana a password like many programs ask at their beginning.
Now, let’s do another user input example. With this example, let’s get a number from the user, and print its square.
The program will ask us;
If we enter 9, the output will be:
Now, with another example, let’s get three input form the user.
The code will ask us the questions and the final output of this code will be like below:
Getting user input is a widely used methods in python programming. Especially in advanced python programs, you will need this python user input function too much. So, you can increase example about this important method of python.