Skip to content
Forge Learn/Python Basics

Input, Output & String Formatting

5 min read

You'll learn to

  • -Read user input with input() and convert it to the right type
  • -Print multiple values with control over separators and line endings
  • -Format numbers precisely using f-strings
  • -Recognize .format() and % formatting as older alternatives you'll still see

print() and input() are how a program talks to whoever is running it. You won't use input() much inside Forge Algorithm Lab levels, since the grader calls your functions directly rather than typing at a prompt - but understanding it is fundamental to Python, and print() remains the tool you'll reach for constantly while debugging.

Try it yourself

print() accepts any number of arguments, joins them with a space by default (controllable via sep), and adds a newline at the end by default (controllable via end). Overriding end is a common trick for building up output across multiple print() calls without new lines splitting them apart.

input(): Reading From the User

Try it yourself

input() prints its argument as a prompt, waits for the user to type something and press Enter, and returns whatever they typed - always as a str, even if they typed digits. That's a common early bug: age = input("...") gives you the string "36", and "36" + 1 raises a TypeError because you can't add a string and an int. You have to explicitly convert it first, with int() or float().

f-strings in Depth

Try it yourself

The part after the colon inside {} is a format spec. .2f means "as a fixed-point float, 2 digits after the decimal" - essential for anything involving money or percentages, where "19.9" reading as "$19.90" matters. The comma adds thousands separators, > and < control alignment, and a leading zero with a width (like 04d) zero-pads integers - handy for things like formatted IDs or times.

Older Formatting Styles (Good to Recognize)

f-strings are the modern, recommended way to format strings, but you'll run into two older styles when reading existing code - worth recognizing even if you rarely write them yourself:

Try it yourself

If you're staring at code with .format() or % in it and it looks unfamiliar, that's normal - it's doing the same job an f-string does, just with older syntax. When writing new code, default to f-strings.

Check Yourself1 / 3

What type does input() always return, regardless of what the user typed?

ScaleDojo Logo
Initializing ScaleDojo