Python 3 – Lesson 02 ( Getting user input )

Python 3 – Lesson 02 ( Getting user input )

As i promised you in my last post, we are going to get user’s input
in this post. We are going to ask user’s name and output some greeting
to him or her. This is really easy and we are going to use input()
function instead  of  raw_input(). Because raw_input() function is
deprecated in python 3 and if you want to use python 2, you can you
raw_input() function. So lets get started. First we are going to declare
a variable to store user’s input. In python, we don’t to tell the
variable type in out code. ( i will make a separate lesson for variables
and variable types ). In below code, i made a variable called “Name”
and i used “Input” function. Inside the input function, im passing the
sentence which we want to print or output to user. In this case, i am
passing “What is your name:”. So this will print first and let user to
input his answer. After that this program will store the input of user
inside the variable called “name”.

name = input('What is your name:')

Now we have user’s input inside our variable. So we are going to output some greeting with user’s name.

name = input('What is your name:') 
print("Hello" + " " + name)

In above code, i am now printing Hello with the name of user. As you
can see, im using print function with the greeting word and our
variable. But we need to add empty space between greeting and the name
of the user. So i made a empty double quotes and inside those double
quotes has a space. it will add a space between greeting text and the
name of the user. So the final output will look like this.

I hope you enjoy my lesson and please remember to comment if you have
a problem about this. I am going to talk about variable types and bit
more about user inputs and outputs with my next lesson.

Leave a Reply

Your email address will not be published. Required fields are marked *