Python · Module 1 · Lesson 51 min read

Input, Output & f-strings

input()

input("prompt") waits for the user and always returns a string. Convert it when you need a number: age = int(input("Age: ")).

  • sep sets what goes between values: print(1, 2, 3, sep="-") → 1-2-3
  • end sets what goes at the end (default is a new line): print("Hi", end="")

f-strings: the best way to format text

Put f before the quotes and variables inside { }:

Python
f"{name} is {age} years old"

Format specs after a colon:

CodeResult
f"{3.14159:.2f}"3.14
f"{1234567:,}"1,234,567
f"{0.256:.1%}"25.6%
f"{'hi':>6}" hi (right-aligned)
f"{x=}"x=5 (great for debugging)

💡 In this app, put the values for input() in the Input box on the output screen, one per line.

▶️ Runs in the app (uses the Input box).

Example

Python
name = input("Enter your name: ")
age = int(input("Enter your age: "))

print(f"{name} will be {age + 1} next year.")

price = 1234.5
print(f"Price: Rs {price:,.2f}")     # Price: Rs 1,234.50
print("a", "b", "c", sep=" | ")     # a | b | c

score = 0.876
print(f"Score: {score:.1%}")         # Score: 87.6%
print(f"{age=}")                     # age=21  (if you entered 21)

# Input box example:
# Asha
# 21