⚡0 XP🔥0

Input and Output

Lesson 7 of 33 ⏱ 5 min read ✎ 3 exercises ❓ 5-question quiz ▶ Runs Python

Read user input with input(), convert it, and display neat output with print(), sep, end and f-strings.

Output with print()#

You have already used print(). Let's learn everything it can do.

Printing several values#

Separate values with commas. print() puts one space between them automatically and converts numbers to text for you:

▶ Try it YourselfPython
Ctrl + Enter to run

The sep parameter#

sep (separator) decides what goes between values. Default is a space " ".

▶ Try it YourselfPython
Ctrl + Enter to run

The end parameter#

end decides what is printed after the last value. Default is "\n" (a new line).

▶ Try it YourselfPython
Ctrl + Enter to run

Escape sequences#

Special characters inside strings start with a backslash \:

Code Meaning
\n new line
\t tab space
\\ a backslash
\" / \' quote inside a string
▶ Try it YourselfPython
Ctrl + Enter to run

f-strings (formatted strings) - the best way to format output#

Put f before the quotes and write variables or expressions inside { }:

▶ Try it YourselfPython
Ctrl + Enter to run

Formatting numbers inside f-strings#

Format Example Result
:.2f - 2 decimal places f"{3.14159:.2f}" 3.14
:, - thousands separator f"{1500000:,}" 1,500,000
:.1% - percentage f"{0.875:.1%}" 87.5%
:>8 - right align in 8 spaces f"{'Hi':>8}" Hi
:<8 - left align f"{'Hi':<8}" Hi
:^8 - centre f"{'Hi':^8}" Hi
:05d - pad with zeros f"{42:05d}" 00042
▶ Try it YourselfPython
Ctrl + Enter to run
Did you know?

Older ways you'll see in other code: "Hello %s" % name and "Hello {}".format(name). They still work, but f-strings (Python 3.6+) are shorter, faster and easier to read.

Input with input()#

input() pauses the program, waits for the user to type something and press Enter, then returns what they typed as a string.

name = input("Enter your name: ")
print("Welcome,", name)

On a computer it looks like this:

Enter your name: Arjun
Welcome, Arjun
Tip

In our browser editor, click + Input and type the input values there (one per line) before pressing Run. Try it below.

▶ Try it YourselfPython
Ctrl + Enter to run

input() always gives a string#

This is the #1 beginner mistake:

a = input("First number: ")    # user types 10
b = input("Second number: ")   # user types 20
print(a + b)                   # 1020  (text joined, not added!)

Convert the input to a number:

a = int(input("First number: "))
b = int(input("Second number: "))
print(a + b)                   # 30
हिंदी में समझें

input() हमेशा string (text) देता है, चाहे user नंबर ही क्यों न टाइप करे। इसलिए "10" + "20" का जवाब 30 नहीं बल्कि "1020" आता है - Python दोनों texts को जोड़ (join) देता है।

हल: int(input()) या float(input()) लिखें - इससे text तुरंत नंबर में बदल जाता है और गणित सही होता है।

Reading several values from one line#

Use .split() to break a line at spaces, and map() to convert each part:

▶ Try it YourselfPython
Ctrl + Enter to run

You'll understand map() fully in the Lambda lesson - for now, remember this pattern.

Complete example: Simple interest calculator#

▶ Try it YourselfPython
Ctrl + Enter to run

Summary#

  • print(a, b, sep=..., end=...) - control spaces and line endings.
  • Escape sequences: \n, \t, \", \\.
  • f-strings: f"{value}", with formats like :.2f, :,, :>10.
  • input() returns a string - convert with int() / float().
  • map(int, input().split()) reads several numbers from one line.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Greet the user

Read a name using input() and print Hello, <name>!. For the test input Kavya the output must be Hello, Kavya!

(Use input() with no prompt text so the output matches exactly.)

Test input:
Kavya
Expected output:
Hello, Kavya!
2

Sum of two numbers

Read two whole numbers (each on its own line) and print their sum.

Test input:
45
38
Expected output:
83
3

Rectangle area with formatting

Read the length and width (decimal numbers) of a rectangle and print the area with 2 decimal places in this format:

Area = 31.50

Test input:
7
4.5
Expected output:
Area = 31.50

Quick Quiz

Q1.

What type does input() always return?

Q2.

What is printed by print("A", "B", "C", sep="-")?

Q3.

What does end="" do in print("Hi", end="")?

Q4.

If price = 49.5, what does f"{price:.2f}" give?

Q5.

User types 5 for x = input(). What is x * 3?