Input and Output in Python

·

5 min read

In Python, to interact with the user input and output, we can use two functions: the input() function and the print() function. Functions are a way to group code and statements so that they can be reused and easily called from other parts of your program. Python functions can take inputs, which are called arguments or parameters, and can also return a value. Functions can be defined with the def keyword, and called by simply using the function name and passing in any necessary arguments. The input() and print() functions are defined within the language itself, so the only thing you need to do is to pass in the arguments to the function. Functions are recognizable from the parentheses that follow the name of the function whenever we decide to invoke it.

input()

To accept input from a user, we need to invoke the input() function. The basic syntax consists of the name of the function followed by parentheses. Inside the parentheses we can type in a prompt, the string we wish to display on the screen. A reason for this could be explaining to the user the kind of input we want, however, the prompt is optional. The function then waits for the user to enter information and press the Enter key.

input(prompt)

When we accept input from the user, we often want to store it in a variable. This way, we can utilize the information the user has given us later in our program.

name = input("What's your name? ")
print("Hello,")
print(name)

It is important to remember that the input function always returns a string, so if you need to operate with other data types, you will have to use typecasting. In the code that follows, we are converting the user input from a string to an integer, to then perform mathematical operations with it.

age = int(input("How old are you? "))
age += 1
print(age)

Notice how in this case we are passing as an argument of the int() function the output of the input() function. Passing the output of a function as an argument of another function is a common practice in all programming languages.

print()

Whenever we want to output something in the console we make use of the print() function. In the past articles we have already made use of this function since it is one of the most basic functions in Python. The syntax consists of the name of the function followed by parentheses. In the previous articles, however, we only passed one argument to print() which consisted of the text we wanted to output. Today, besides the object parameter, we are going to take a deeper look at the sep and end parameters.

A parameter is a variable that is declared when defining a function and is used to accept input values. When a function is called, values can be passed into the function's parameters, which are then used as input values for executing the code inside the function. I get it, it sounds very difficult, but don't worry about comprehending the entire concept. As time passes, you will become more and more familiar with parameters and other complex programming concepts.

Here is the basic syntax of the print() function. As I said before, today we are going to look at the sep and end parameters, but I want you to know that print() also has other parameters.

print(objects, sep=' ', end='\n')

print() parameters:

  • objects: indicates the object to be printed. Additionally, the function is capable of printing more than one object

  • sep: indicates the separation between objects. The default value is ' ', which means that objects are separated by a space

  • end: indicates the end of the object. The default value is '\n', which means that at the end of the objects, a new line will be printed

Until now, we have passed in the print() function only one object as an argument. Whether the object consisted of a string or a variable to be outputted, the number of arguments has always been one.

If we want to pass two objects into the function, we need to separate them with a ,. In the example below, we are going to pass as arguments a string and a variable name. Keep in mind that since we are not specifying the sep and end parameters, they assume their default value.

user_name = "John"
print("hello", user_name) # Output => hello John

Setting sep and end parameters

As I have previously written, whenever you do not specify a value for the sep and end parameters, they assume their default value. This does not mean that they are unchangeable, we can modify them by explicitly writing the value we want them to assume. The place where we perform this operation is in the parentheses that follow the function. In the example below, we are overriding the sep parameter and setting its value to -.

user_name = "John"
print("hello", user_name, sep='-') # Output => "hello-John"

As for the sep parameter, we can modify the default value of the end parameter. Its default value '\n' means that a new line is printed every time we invoke the print() function. In the example below, we are overriding the end parameter and setting its value to .. If you are running the example on VS Code, you can notice how the cursor is moved right next to the output and not on a new line.

user_name = "John"
print("hello", user_name, end='.') # Output => hello John.

Conclusion

In this article, you have learned the basics of input and output in Python. Understanding how to use input() and print() effectively is critical for creating user-friendly programs and providing a positive user experience. Many fundamental concepts of programming were introduced above, such as functions and parameters. Don't aspire to gain a complete understanding by the time you have terminated this read, you will assimilate these concepts better as you keep learning Python.