CS 111 › Lesson 2 of 12

Input, Output & String Formatting

Lesson 2 · OKSTEM College · Associate of Science in Computer Science

Input, Output & String Formatting

Python's built-in print() and input() functions handle console I/O. Modern Python uses f-strings for clean string formatting.

# Basic I/O name = input("Enter your name: ") print("Hello,", name) # F-strings (Python 3.6+) age = 20 gpa = 3.875 print(f"{name} is {age} years old. GPA: {gpa:.2f}") # Output: Alice is 20 years old. GPA: 3.88 # Format spec mini-language print(f"{42:08b}") # 00101010 (binary, width 8) print(f"{1234567:,}") # 1,234,567 (thousands separator) print(f"{0.1234:.2%}") # 12.34% (percentage) # Multi-line & separator print("a", "b", "c", sep="-", end="!\n") # a-b-c!

input() always returns a string. Convert with int() or float() before arithmetic: age = int(input("Age: "))

Lab — F-String Format Playground

Knowledge Check

What does input() always return?

The f-string f'{3.14159:.2f}' produces

How do you print items separated by commas in print()?

f'{255:#x}' formats 255 as

To read a number from the user, which is correct?

← PreviousNext →