Learn

Short lessons with real code you can edit and run on the page.

Input and Output in Python

Lesson 7 Python 3.14 Runs in your browser Updated
In short

input() pauses the program, waits for the user to type a line and returns it as a string. print() writes values to the screen, separated by spaces and followed by a line break, and both of those defaults can be changed with the sep and end arguments.

Key facts

  • input() always returns a string, even when the user types a number. Convert with int() or float().
  • The text passed to input() is the prompt, printed without a line break.
  • print("a", end="") keeps the next output on the same line. The default end is a line break.
  • print(*range(1, 11)) prints 1 to 10 in one call.

How do I get input from the user in Python?

Call input() with the question as its argument. The program shows the question, waits for a line of typing, and hands the text back when the user presses Enter. On this page, each example that calls input() reads from the input box under its code, which already holds a sample answer you can change before you press Run.

Python
name = input("What is your name? ")
print(f"Hello, {name}!")
Output
What is your name? Hello, Ada!

In a terminal the typed name appears after the prompt. In the output panel here it doesn't, which is why the greeting shows up on the prompt's line.

Does input() always return a string?

Yes. Whatever the user types comes back as a str, even when it looks like a number, so math on it fails or goes wrong until you convert it. "36" * 2, for example, gives "3636".

Python
age = input("How old are you? ")
print(type(age))
print(age + 1)
Output
How old are you? <class 'str'>
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    print(age + 1)
          ~~~~^~~
TypeError: can only concatenate str (not "int") to str

Wrap the call in int() or float() and you get an actual number right away. If the text isn't a whole number, like 3.5 or ten, int() raises a ValueError, and a later lesson shows how to catch it.

Python
age = int(input("How old are you? "))
print(f"Next year you will be {age + 1}.")
Output
How old are you? Next year you will be 37.

Pass end="" to print(). The end argument is the text printed after the values, and it defaults to a line break. The sep argument is the text printed between values, and it defaults to one space.

Python
print("Loading", end="")
print("...", end="")
print(" done")
print("a", "b", "c", sep="-")
print("2026", "09", "16", sep="/")
Output
Loading... done
a-b-c
2026/09/16

Unpack a range into print() with a star. range(1, 11) produces the numbers 1 through 10, the star passes them as separate arguments, and sep decides what goes between them.

Python
print(*range(1, 11))
print(*range(1, 11), sep=", ")
Output
1 2 3 4 5 6 7 8 9 10
1, 2, 3, 4, 5, 6, 7, 8, 9, 10

A for loop can also print the same numbers one at a time. Loops come later in the course.

Three ways give the same line. Passing several arguments lets print() add the spaces, an f-string gives you full control, and + works only when everything is already a string.

Python
name = "Ada"
score = 42
print("Name:", name, "Score:", score)
print(f"Name: {name} Score: {score}")
print("Name: " + name + " Score: " + str(score))
Output
Name: Ada Score: 42
Name: Ada Score: 42
Name: Ada Score: 42

The f-string is the one to reach for. It reads like the output and doesn't need str() to mix text with numbers.

Common mistakes with input and print in Python

Three of these four mistakes with input() and print() come back to the same fact, that input() returns text.

  • TypeError: can only concatenate str (not "int") to str. The value from input() is text. Convert it with int() or float() before doing math.
  • Comparing input to a number. answer == 5 is always False when answer came from input(). Compare with "5", or convert first.
  • ValueError: invalid literal for int() with base 10. The text isn't a whole number, like 3.5 or ten. Check the text first, or catch the error, which a later lesson covers.
  • A prompt glued to the answer. input("Age?") leaves the cursor right after the question mark. End the prompt with a space, or with \n to put the answer on the next line.
Python
answer = input("Type 5: ")
print(answer == 5)
print(answer == "5")
Output
Type 5: False
True

Exercise

Replace the four print lines with two so the program prints 1-2-3 Done on a single line. The first call prints the three numbers with hyphens between them and a space at the end instead of a line break, and the second prints Done.

Python
print(1)
print(2)
print(3)
print("Done")
Output
Show the solution
print(1, 2, 3, sep="-", end=" ")
print("Done")

Quiz

This quiz has 4 questions. Pick an answer to see why it is right or wrong.

  1. 1The user types 42 at an input() prompt. What type is the value the program receives?

  2. 2What does this program print?

    print("a", "b", sep="")
    print("c")
  3. 3What does this program print?

    print("x", end="")
    print("y")
  4. 4Which line reads a whole number from the user?

Frequently asked questions

Does input() always return a string in Python?
Yes. input() returns whatever was typed as a str. Wrap it in int() or float() to get a number, and expect a ValueError when the text can't be converted, such as "3.5" for int() or "ten" for either.
What does input() do in Python?
It prints an optional prompt, waits for the user to type a line and press Enter, and returns that line as a string without the line break.
How do I take a number as input in Python?
Wrap input() in a conversion, such as age = int(input("Age? ")) for whole numbers or price = float(input("Price? ")) for decimals.
How do I print without a newline in Python?
Pass end="" to print(), as in print("Loading", end=""). The next print continues on the same line.
How do I print on the same line in a loop in Python?
Use print(item, end=" ") inside the loop so each value is followed by a space instead of a line break, then call print() once after the loop to finish the line.
What are input() and print() used for in Python?
They are the simplest way for a program to talk to the person using it. print() shows text on the screen and input() reads a line the person types. Many beginner programs use both.