Skip to content
Forge Learn/Python Basics
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

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 will still see

Tip: the highlighter is on - just select any text below to mark it. Use the highlighter button up top to change color or turn it off, saved just for you on this device.

print() and input() are how a program talks to whoever is running it. You will not 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 will 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 is a common early bug: age = input("...") gives you the string "36", and "36" + 1 raises a TypeError because you cannot 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 will run into two older styles when reading existing code, worth recognizing even if you rarely write them yourself.

Try it yourself

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

Interview Signal

A beginner writes age = input("How old are you? ") followed by print(age + 1), and gets a TypeError. What is actually wrong?

Weak Answer

"Probably a typo somewhere in the input() call."

Strong Answer

"input() always returns a str, even when the user types digits, so age is the string '36', not the integer 36, and Python will not silently add a string and an int together. The fix is converting it explicitly first, with int(age) + 1. This is one of the most common early bugs in Python specifically because the return type of input() is easy to forget once you're focused on the logic that comes after it."

Check Yourself1 / 4

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

Module Assignment

Celsius to Fahrenheit

Write a function celsius_to_fahrenheit(c) that converts a Celsius temperature to Fahrenheit and returns the result. The formula is F = C * 9/5 + 32. This is the module's own checkpoint - variables, numbers, and operators are all it needs.

Your solution