agyanadda@gmail.com

Python Input and Output

Python Basics 1 min read Updated Dec 4, 2026 Topic 9 of 32

print()

  • print("a", "b", 3) — separates values with a space
  • sep="-" — change the separator
  • end="" — don't move to a new line
  • print() alone prints an empty line
  • file=sys.stderr — print errors to the error stream

input()

name = input("Your name: ") shows a prompt and waits for the user. It always returns a string.

Converting input

  • age = int(input("Age: "))
  • price = float(input("Price: "))
  • Several values on one line: a, b = map(int, input().split())

Handling bad input

If the user types abc, int() raises ValueError. Use a loop with try / except to ask again (see the Exceptions lesson).

Nice output

Use f-strings with alignment to print tables and receipts.

Example

Example — Simple bill calculator Python
item = input("Item name: ")
price = float(input("Price: "))
qty = int(input("Quantity: "))

subtotal = price * qty
gst = subtotal * 0.18

print("-" * 30)
print(f"{'Item':<15}{item:>15}")
print(f"{'Subtotal':<15}{subtotal:>15.2f}")
print(f"{'GST 18%':<15}{gst:>15.2f}")
print(f"{'Total':<15}{subtotal + gst:>15.2f}")
print("-" * 30)

print("Thank", "you", sep="-", end="!\n")

Entering Pen, 20, 5 prints a neat receipt: Subtotal 100.00, GST 18% 18.00, Total 118.00, then Thank-you!

Test Yourself With Exercises

Exercise 1 Multiple Choice

What type does input() return?

Exercise 2 True / False

print("a", "b", sep="-") prints a-b.

Exercise 3 Fill in the Blank

Read two numbers on one line: a, b = ___(int, input().split())

Frequently Asked Questions

input() returns a string, and Python cannot add a number and a string. Convert first: 5 + int(input()).