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
print("Total:", 250, "rupees")
print("a", "b", "c", sep="-")
print("Loading", end="...")
print("done")
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
name = input("What is your name? ")
print("Vanakkam,", name)
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.
age = int(input("Your age: "))
print("Next year you will be", age + 1)
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
- Ask for two numbers and print their sum.
- Ask for the user's name and city, then print one sentence using both.
- Print the numbers 1, 2 and 3 on one line separated by
|using a single print andsep.
Create a free account to save your progress, or sign in.