Skip to main content
Practice

How to Talk to a Computer: Input and Output

Computer programs interact with users and perform tasks based on their requests.

To do this, programs receive data from the user and the external world as input and return results as output.

In Python, the input() function is used to receive input from the user, and the print() function is used to output results.


Getting Input from the User with input

When you need to ask the user something and receive a response, use the input() function.

For example, you can ask, "What is your name?" and have the user input their name through the program.

Getting User Input
name = input("What is your name? ")

print(name)

When you run the code above, the computer will ask "What is your name?" and print the name entered by the user.


Note: The input() function always returns the input as a string.

If you want to handle numerical operations after receiving numbers from the user, you should wrap the input value with the int() function to convert it to a number.

Processing Numerical Input
# Convert the input age to an integer using int
user_age = input("How old are you? ")
age = int(user_age)

# Add 1 to the input age to calculate the age next year
next_year = age + 1

# Print the age next year: input age + 1
print(next_year)

Displaying Results with print

As seen in the previous example, to make the computer output something, use the print() function.

This function is used to display results on the screen, and you can also print multiple values at once.

Printing
print("Python is", "really", "fun!")

In the code above, multiple strings are separated by commas and printed on one line, with the comma-separated values connected by a space.

Therefore, this code will print "Python is really fun!".

We will learn more about the print function in the next lesson.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.