Input and Output Variables
We'll cover the following
Inputting variables
Python allows users to input information through the console and save this data in a defined variable. For example, let's create a variable named user_name
and obtain the user's name during runtime using the input method. The code prompts the user with "Please enter your name = " and, upon receiving the input, saves it in user_name
.
user_name = input("Please enter your name \n")
Enter the input below
Outputting variables
Another crucial aspect is outputting a specific variable or result. This can be easily achieved by incorporating the print()
method into our code and passing our defined variable to it. For instance, we can print a predefined user_name
to the console using the following code.
user_name = "My Name"print("Hello " + user_name + "!")
Note: The "+" operator in the context of strings is used to concatenate the sequence of characters together.
Combining input and output
Consider the following example, where the user is prompted by the code to input their name using the input()
method. After the input is received, the program outputs a greeting message along with the entered name, through the print()
function.
user_name = input("Please enter your name \n")print("Hello", user_name + "!")
Enter the input below