Most real-world applications depend on user inputs and change their execution logic based on those inputs. Their outputs can depend on what the user inputs into the program. So far we’ve only looked at programs that print fixed outputs.
print()is used to print output.
input(), on the other hand, is used to get input from the user. The user can enter their name, a phone number, some message, etc.
So, if we would like to write a simple program that would greet the user we should store the user input in some variable and print it later (we will discuss variables in more detail later):
name = input() print('Hi', name)
Here the program prints the fixed text
Hifollowed by the value stored in the variable called
name. In case the user inputs
Anna, the program would print
Hi Anna. In case the user inputs
Simon, the program would print
Hi Simon. So, the same program prints different things depending on what the user inputs.
nameis assigned to
input(). This tells Python to store the value entered by the user in the variable called
name. After that, we are able to print the
namelike any other text or number. Pay attention that to print the value stored in the variable, we don’t use quotes and instead write the name of the variable.
Write a program that would greet the user with
Hello,and then the name of the user.
Here are some examples of how the program should behave for different inputs: