Lesson 6 of 6 · Getting Started · 2 min read · Updated

Input and output

Asking the user a question, and printing the answer exactly the way you want.

More from print

python
print("Total:", 250, "rupees")
print("a", "b", "c", sep="-")
print("Loading", end="...")
print("done")
Output
Total: 250 rupees
a-b-c
Loading...done

Give print() several things separated by commas and it prints them with a space between. sep changes that separator. end changes what comes after — normally a new line, which is why each print usually starts a fresh line.

Asking the user

python
name = input("What is your name? ")
print("Vanakkam,", name)
Output
What is your name? Kavya
Vanakkam, Kavya

input() shows its message, waits for the user to type and press Enter, and gives back what they typed.

Warning

input() always gives back text, even when the user types a number. "5" + "3" is "53", not 8. Convert it first with int() for whole numbers or float() for decimals.

python
age = int(input("Your age: "))
print("Next year you will be", age + 1)
Output
Your age: 19
Next year you will be 20

Note

If the user types something that is not a number, int() raises a ValueError. The Errors topic shows how to catch that and ask again.

The user types 10 at n = input("n: "). What is n * 2?

Try this now

  1. Ask for two numbers and print their sum.
  2. Ask for the user's name and city, then print one sentence using both.
  3. Print the numbers 1, 2 and 3 on one line separated by | using a single print and sep.

Create a free account to save your progress, or sign in.