Input & Output: input(), print(), f-strings

ইনপুট ও আউটপুট — input, print, এবং f-strings

Read: ~22 min Beginner 5 practice problems Live code runner

1. Talking to the User

Every non-trivial program needs a way to accept input and display output. Python keeps both simple: input() reads a line from the keyboard, print() writes to the screen. Around these two basics sits a rich formatting system — topped by the modern f-string.

প্রায় সব প্রোগ্রামেই ব্যবহারকারীর কাছ থেকে ইনপুট নেওয়া এবং স্ক্রিনে আউটপুট দেখানো লাগে। Python-এ এটি খুব সহজ: input() কিবোর্ড থেকে এক লাইন পড়ে, print() স্ক্রিনে লেখে। এই দুই ফাংশনের চারপাশে একটি সমৃদ্ধ formatting system রয়েছে — যার সবচেয়ে আধুনিক রূপ হলো f-string।

2. input() — Always Returns a String

input(prompt) shows the prompt, waits for the user to press Enter, and returns the typed line as a string — even if the user typed a number. Convert to int or float when you need arithmetic.

input() সবসময় string return করে — সংখ্যা টাইপ করলেও। গণনার জন্য int(input()) বা float(input()) করে convert করতে হবে।
input_demo.py
age_text = input("Your age: ")
age = int(age_text)
print(f"Next year you will be {age + 1}")

3. print() — All Its Parameters

The full signature is print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False).

print_opts.py
print("one", "two", "three")
print("one", "two", "three", sep=" | ")
print("no newline", end=" → ")
print("continued")

# Print to stderr
import sys
print("error message", file=sys.stderr)

4. f-strings — The Modern Way (PEP 498)

Introduced in Python 3.6, f-strings are the preferred way to format strings. Prefix a string with f and you can embed any expression inside { }.

fstrings.py
name = "Sadia"
score = 87.345

# Basic interpolation
print(f"Hello, {name}!")

# Expressions inside braces
print(f"Next year: {score + 5:.2f}")

# Width and alignment
for item in ["rice", "fish curry", "dal"]:
    print(f"{item:<15} | ready")

# Percentage, hex, binary
print(f"{0.756:.1%}")
print(f"{255:#x}")
print(f"{10:08b}")

# Debugging self-documenting (Python 3.8+)
x = 42
print(f"{x=}")

5. Older Formatting Styles

You will see these in legacy code — know how to read them.

old_format.py
name, score = "Rafiq", 92

# Old % style (C-like)
print("%s scored %d" % (name, score))

# .format() method
print("{} scored {}".format(name, score))
print("{name} scored {score}".format(name=name, score=score))

# Modern f-string — always prefer this
print(f"{name} scored {score}")

6. Reading Multiple Inputs

Common pattern for competitive programming: read several integers separated by spaces.

multi_input.py
# Read three numbers from one line
a, b, c = map(int, input().split())
print(f"Sum = {a + b + c}")

# Read a list of N numbers
# n = int(input())
# nums = list(map(int, input().split()))

7. Vocabulary (শব্দভাণ্ডার)

TermMeaningবাংলায়
stdin / stdout / stderrStandard input / output / error streams.স্ট্যান্ডার্ড ইনপুট / আউটপুট / এরর স্ট্রিম।
InterpolationInserting values into a string template.String-এর ভেতর মান বসানো।
Format specThe :... suffix inside {} that controls formatting.{}-এর ভেতর :-এর পরের অংশ — formatting নিয়ন্ত্রণ করে।
map()Applies a function to each item of an iterable.Iterable-এর প্রতিটি element-এ ফাংশন প্রয়োগ করে।
split()Splits a string on whitespace (or a given separator).String-কে whitespace (বা নির্দিষ্ট separator)-এ ভেঙে দেয়।

8. Practice Problems

  1. Ask for the user's name and greet them with an f-string.
    ব্যবহারকারীর নাম নিয়ে f-string দিয়ে তাকে greet করুন।
    ✨ Show Answer (উত্তর দেখুন)
    ans1.py
    name = input("Enter your name: ")
    print(f"Hello {name}! Welcome to Python.")
  2. Read two numbers on separate lines and print their product.
    দুটি আলাদা লাইনে সংখ্যা পড়ে তাদের গুণফল প্রিন্ট করুন।
    ✨ Show Answer (উত্তর দেখুন)
    ans2.py
    a = int(input())
    b = int(input())
    print(f"{a} × {b} = {a * b}")
  3. Print the value of π (math.pi) rounded to 4 decimal places using an f-string.
    f-string ব্যবহার করে π এর মান (math.pi) 4 decimal পর্যন্ত প্রিন্ট করুন।
    ✨ Show Answer (উত্তর দেখুন)
    ans3.py
    import math
    print(f"π ≈ {math.pi:.4f}")
  4. Print three items side by side, each padded to 10 characters wide.
    তিনটি item পাশাপাশি প্রিন্ট করুন, প্রতিটি 10 character চওড়া।
    ✨ Show Answer (উত্তর দেখুন)
    ans4.py
    items = ["rice", "fish", "dal"]
    for it in items:
        print(f"{it:<10}", end="")
    print()
  5. Read a line of space-separated integers and print their sum.
    একটি লাইনে space দ্বারা বিভক্ত integer পড়ে তাদের যোগফল প্রিন্ট করুন।
    ✨ Show Answer (উত্তর দেখুন)
    ans5.py
    nums = list(map(int, input().split()))
    print(f"Sum = {sum(nums)}")

Summary — Module 08

input() always returns a string; print() takes sep, end, and file keyword arguments for flexibility. For formatting, use f-strings — they are fast, readable, and expressive. You will see % and .format() in older code, but new code should prefer f-strings.

input() সবসময় string return করে; print()-এ sep, end, file — এই keyword argument ব্যবহার করা যায়। Formatting-এর জন্য f-string ব্যবহার করুন — এটি দ্রুত, পাঠযোগ্য ও শক্তিশালী।

Next Module → Control Flow I — if, elif, else, match।