Input and Output

Learn how to print and get input from the user in Python.

Ali Berro

By Ali Berro

4 min read Section 3
From: Python Fundamentals: From Zero to Hero

Input and Output

The print function

Any programming language should at least support the bear minimum; letting the user print on the screen. Python has a the print built-in function. Syntax is as follows:

print(value, ..., sep=' ', end='\n', file=None, flush=False)

I know this looks intimidating at first, but let’s look at a simpler version of this function, then we can build our way to this monstrosity. We will take a look at this simpler version for now.

print(value, ..., sep=' ', end='\n')

Still looks bad? How about this one:

print(value, ...,)

Python can print one or more values, by separating them with the comma ,.

print(1) #1
print("Hello, I'm learning Python") #Hello, I'm learning Python
print("This course", "Is AMAZING") #This course Is AMAZING
x = 1
y = 2
z = 3
print(x, y, z) #1 2 3
print() #prints a new line

As we can see, we can add one or more values to the print function, and it will gladly write them to the console window. One thing that might be interesting to you is that each value is separated by a space, now why is that and can we change this behavior?

The sep

In the print function, there are a couple of default parameters, which is something that has a default value, the sep is (by default) set to a space, we can override it by writing sep='something-else?'.

x = 1
y = 2
z = 3
print(x, y, z) #1 2 3
print(x, y, z, sep='') #123
print(x, y, z, sep='Hello') #1Hello2Hello3

Notice now how the values are separated, by a space, nothing, and the string 'Hello'. sep works by adding the string between the values.

sep must be a string, otherwise it will raise a TypeError.

The end parameter

After answering that question, we can safely go to another set of questions, why when we print, we go to the next line, and can we change that? This can also be changed by overriding the end value, which is set by default to \n. \n represents a newline character, which means create a new line after printing the values.

print(1, end='') #Prints 1 but doesnt add a new line
print(2, end='A') #prints 2 followed by the character A
print(3) #prints 3 followed by a new line
print(4, end='B') #prints 4 followed by the character B

So it will output 12A3 followed by a newline, followed by 4B, or as a whole string 12A3\n4B.

Note

In the string section, we will be talking all about \n along with its cousins \t, \r, and so on.

The input function

To create amazing things, we need user interaction. The simplest way to do this is via the input function. The input function is used to get input from the user. It returns a string, which is the input from the user. Syntax is as follows:

input(prompt='')

The prompt is a string that is displayed to the user. It is optional, and if not provided, it will not display anything. However, if provided, it will be displayed as-is without any newlines.

input-example.py
name = input("What's your name? ")
print("Hello,", name, "!") #Hello, Ali!
input-example-2.py
age = input("How old are you? ")
print("You are", age, "years old!") #You are 18 years old!

Note

Notice how I added a space after the ? in the prompt, this is because I dont want the user’s input to be sticking to the prompt.

It always returns a string, even if the user inputs a number.

Exercises

Exercise 1: Single Input

Write a program that asks the user to enter his name, then say hello to him.

Answer:
name = input("Enter your name: ")
print("Hello,", name)

Exercise 2: Multiple Inputs

Write a program that asks the user to enter his favorite color, and ice cream flavor, then say “I like [color] and [flavor] too!”.

Answer:
color = input("Enter your favorite color: ")
flavor = input("Enter your favorite ice cream flavor: ")
print("I like", color, "and", flavor, "too!")

Exercise 3: Multiple Inputs with Print Manipulation

Write a program that asks the user for their name and age, and then prints a message saying “Hello, [name]! You are [age] years old.”

Answer:
name = input("What's your name? ")
age = input("How old are you? ")
print("Hello, ", name, "! You are ", age, "years old.", sep="")

Course Progress

Section 3 of 17

Back to Course