Programming and Computation · Module 3 of 10

Input, Output, and Formatting

A result you cannot read clearly is a result you cannot trust. This module covers printing values, formatting numbers to a sensible precision with f-strings, and reading input, which always arrives as text.

01

Readiness check

This module makes results readable. Tick only what you can do closed-notes.

  • Recall rounding to a set number of decimals.
  • Recall significant digits from the orientation course.
  • Use variables and the calculator from Modules 1 and 2.
  • Recall that text and numbers are different types.
  • Recall that a report should carry units.
0 or 1 weak itemsContinue with this module.
2 weak itemsRevisit types in Module 2.
3 or more weak itemsRevisit significant digits in Orientation, Module 3.
02

The core idea

Use print() to show values and f"..." strings to embed them with a chosen precision, so engineering output is clean and labeled. Input from a user or a file always arrives as a string, so you must convert it with float() or int() before doing arithmetic.

print('F =', force, 'N')f"{value:.2f}" formats to 2 decimalsinput() returns a string

Once you can compute, you must communicate. The print() function displays one or more values, separated by spaces, so print('Force =', force, 'N') writes a labeled result. Raw floats print with all their digits, which is rarely what a report wants, so Python provides formatted strings, or f-strings: prefix a string with f and put an expression in curly braces, optionally with a format specifier after a colon. The specifier :.2f means "fixed-point, two decimals," so f"{stress/1e6:.2f} MPa" turns 100000000 pascals into the tidy "100.00 MPa." Other specifiers give scientific notation or set a field width, matching the significant-digits habit from earlier courses. The other half of input and output is reading data. The input() function, and text read from a file, always returns a string, even if it looks like a number: "12.5" is text, and multiplying it as a number fails until you convert it with float(). Forgetting that conversion is one of the most common beginner errors, and remembering it makes your programs read data and report results cleanly.

The skill works when: you format numbers to a sensible precision and convert input text before computing.
The skill breaks down when: raw floats flood the output, or string input is used in arithmetic without conversion.
The concept. An f-string embeds a value with a format specifier, turning a raw float into a clean, labeled engineering result.
03

The skills, taught in order

Five skills make output clear and input safe.

3.1 Printing values

print() shows values separated by spaces and adds a newline. You can mix text and numbers: print('F =', force, 'N'). This is enough for quick checks while you develop.

3.2 f-strings

Prefix a string with f and put an expression in curly braces to embed it: f"radius = {r}". F-strings keep the label and the value together in one readable line.

3.3 Format specifiers

After a colon inside the braces, a specifier sets the format: :.2f for two decimals, :.3e for scientific notation, :8.2f for a width of eight. This is how you apply significant-digit discipline to output.

SpecifierExample on piResult
:.2ff"{math.pi:.2f}"3.14
:.3ff"{math.pi:.3f}"3.142
:.3ef"{12345:.3e}"1.234e+04

Common format specifiers. The colon separates the value from how it is shown.

3.4 Reading input as text

input() returns a string, always. To compute with it, convert: x = float(input()). The same is true of text read from files (Module 10). Convert first, compute second.

3.5 Building a result line

Combine a label, a formatted value, and a unit into one f-string: f"stress = {sigma/1e6:.2f} MPa". A consistent result line makes a program's output self-explanatory.

Engineering connection: a calculation script ends with formatted, labeled, unit-carrying lines that a reviewer can read at a glance, exactly the reporting habit from the measurements and orientation courses.

04

Worked example 1: formatting a stress

A stress of 100000000 pascals should print as megapascals with two decimals.

sigma = 1e8              # pascals
print(sigma)             # raw
print(f"{sigma/1e6:.2f} MPa")

Output:

100000000.0
100.00 MPa
  1. ProblemPrint a 1e8 Pa stress cleanly in MPa.
  2. Given / findsigma = 1e8 Pa. Find a two-decimal MPa string.
  3. ModelDivide by 1e6 for MPa, format with :.2f.
  4. Code1e8 is scientific notation for 100000000.0; the f-string embeds the converted value.
  5. SolveThe formatted output is 100.00 MPa.
  6. Check1e8 pascals is 100 megapascals, matching.
  7. ConclusionFormatting turns a raw float into a labeled, report-ready line.
Result. 100.00 MPa.
05

Worked example 2: pi to three decimals

Print the constant pi to three decimals inside a labeled sentence.

import math
print(f"pi is about {math.pi:.3f}")

Output:

pi is about 3.142
  1. ProblemShow pi to three decimals in a sentence.
  2. Given / findmath.pi. Find a three-decimal labeled string.
  3. ModelEmbed math.pi with the :.3f specifier.
  4. SolveThe output reads pi is about 3.142.
  5. CheckPi is 3.14159..., which rounds to 3.142 at three decimals.
  6. ConclusionThe colon-specifier controls exactly how many digits appear, matching your significant-figure needs.
Result. "pi is about 3.142".
06

Misconceptions and diagnostics

MistakeSymptomDiagnostic questionCorrection
Using input as a numberA type error when doing arithmetic"Did I convert with float()?"Convert input text before computing.
Raw float outputLong, unreadable numbers"Did I add a format specifier?"Format with :.2f or similar.
Adding text to a numberA type error concatenating"Are both sides strings?"Use an f-string to combine label and value.
Over-precise reportsTwelve digits where two suffice"How many digits does the data support?"Match the format to the significant digits.
07

Practice ladder

Level 1 · Direct skill

Format 9.80665 to two decimals with an f-string.

Show answer

f"{9.80665:.2f}" gives "9.81".

Level 2 · Mixed concept

Predict the output of f"{1234.5678:.1f}".

Show answer

"1234.6", rounded to one decimal.

Level 3 · Independent problem

A script reads a radius with r = input(). Why does math.pi * r ** 2 fail, and how do you fix it?

Show answer

input() returns a string, so the power operator fails. Fix it with r = float(input()) before computing.

Transfer task | Real engineering

Compute a beam deflection and print it as a labeled result line with three significant figures and units.

What good work looks like

For example print(f"deflection = {delta*1000:.2f} mm"). A good answer converts to a sensible unit, applies a format specifier, and labels the value.

08

Working with AI, and proving it yourself

Use AI as a tutor, not a black box

"Explain what :.2f and :.3e do in an f-string."
"Give me values; I will predict their formatted output, then run them."
"Format all my output." Learn the specifiers yourself.
"Why does my input break?" Recall that input is text, then convert.

Portfolio task

Turn a past calculation into a script that reads one input, computes, and prints a labeled, correctly-rounded result line.

Must include: a float(input()) conversion, an f-string with a specifier, and a unit label.
09

Retrieval and spaced review

Closed notes. Answer out loud, then reveal.

1. What does :.2f do?

Formats a number as fixed-point with two decimals.

2. What type does input() return?

A string, always.

3. How do you embed a value in text?

An f-string: f"x = {x}".

4. How do you use input in arithmetic?

Convert first, for example float(input()).

5. Why format output?

To match significant digits and make results readable and labeled.

TodayFinish this quiz and Levels 1 and 2 of the ladder.
+1 dayReformat one raw output into a clean line.
+3 daysWrite a read-compute-print script.
+7 daysMove on to making decisions in Module 4.
+30 daysEnd every script with formatted, labeled results.
10

Textbook mapping

This module follows Hans Fangohr, Introduction to Python for Computational Science and Engineering. Use these references to read further.

Topic in this moduleWhere to read more
Printing to outputFangohr, Section 5.1, Printing to standard output
String formattingFangohr, Chapter 5, Input and Output
Reading input and filesFangohr, Section 5.2, Reading and writing files

Chapter numbers refer to Fangohr's Introduction to Python for Computational Science and Engineering. Any recent version is equivalent for study.