Take User Inputs In Python – Chapter 4

We know how to print something on the screen using python in the previous chapter. So know it’s time to take inputs from user.

What we are going to do today

Today we are making our second program in python to learn about user inputs in python programming. In today’s program we are going to ask user for their name and greet them hello followed by their name.

Start with opening the python text editor by running the python IDLE and then pressing Ctrl+N to open new text editor. Let’s write our amazing code now.

You can also follow video tutorial provided below with this blog.

Our Code (User Inputs In Python)

This is the code for our program to greet the user with their name. I am using Python3.

name = input("Please enter your name: ")
print("Hello, " + name)

That’s it. This our today’s program code. Above code is briefly explained below in the next paragraph.

Code Explanation

name = input("Please enter your name: ")

Our first line of code as user to enter their name. input() is also a python function used for collecting inputs from the users. In the above line of code we added some text between parenthesis(brackets). That text will be displayed to the user as an helping message to understand what you really want user to add when you are asking for the inputs. At last, we are storing the input in the name variable. We learn more about variables later in this course. But at this stage remember that variables are used to store some values in them so that we can get that value back by using that variable in which we are stored it. Pretty cool.

print("Hello, " + name)

This line is responsible for greeting the user with their name. We used python print() function here. Refer to this link to learn more about print() function. We entered our message between parenthesis(brackets). We are saying print function to say “Hello” and then we used the concatenation (plus symbol) and then used the variable name. Remember this variable? This is the variable in which we store the user input or in other words their name. Now we are using that value in print() function to greet user with their name. 

Testing the Program

To test our program, is it actually works or not. Press F5 in your python file to run the code. If everything works fine then it will asks you for your name. Enter your name and press “Enter” key. Otherwise, if any error comes up comment it below or feel free to reach me via my social handles.

Leave a Reply

Close Menu