LearningPython TutorialInput & Output
CHAPTER 5 · PYTHON BASICS

Python Input & Output

Programs useful tab bante hain jab woh user se data le sakein aur clear output dikha sakein. Is chapter me print(), input(), separators, line endings, escape sequences, f-strings, number formatting aur small console programs practical examples ke saath samjhenge.

English + Hinglish50 min readOnline practice included

Output with print()

print() values ko standard output par display karta hai.

print-basic.pyPython
print("Hello, BrounStack!")
print(42)
print(True)
Hinglish Explanation

print() ke andar text, number, boolean ya variables de sakte ho. Python unhe readable form me console par show karta hai.

Try print() →

Print multiple values

print() multiple arguments accept karta hai. By default unke beech ek space insert hota hai.

multiple.pyPython
name = "Aman"
age = 21
course = "Python"

print(name, age, course)

Control separator with sep

sep multiple printed values ke beech ka separator control karta hai.

separator.pyPython
print("2026", "09", "12", sep="-")
print("HTML", "CSS", "JavaScript", sep=" | ")

Default sep=" " hota hai.

Control line ending with end

Normally print() ke end par newline add hoti hai. end se ise change kar sakte ho.

end.pyPython
print("Loading", end="...")
print("done")

Output same line par Loading...done hoga.

Escape sequences

Backslash-based escape sequences string ke andar special characters represent kar sakti hain.

escapes.pyPython
print("Line 1\nLine 2")
print("Name:\tAman")
print("He said \"Hello\"")
print("C:\\Users\\Student")
  • \n — new line
  • \t — tab
  • \" — double quote inside double-quoted string
  • \\ — literal backslash

Read user input with input()

input() standard input se text read karta hai. Prompt string optional hai.

input-basic.pyPython
name = input("Your name: ")
print("Hello,", name)

Try input() with stdin →

input() returns a string

Important beginner rule: input() normally text yani str return karta hai, even if user digits type kare.

input-type.pyPython
age = input("Age: ")
print(age)
print(type(age))
Remember: User 21 type kare tab bhi returned value initially string hoti hai.

Convert numeric input

Calculation ke liye input ko intended numeric type me explicitly convert karo.

numeric-input.pyPython
age = int(input("Age: "))
price = float(input("Price: "))

print(age + 1)
print(price * 2)
Invalid input: Agar user expected number ke badle invalid text de, conversion ValueError raise kar sakti hai. Exception handling Chapter 14 me detail me aayega.

Formatted output with f-strings

f-strings readable way se variables aur expressions ko string me embed karti hain.

fstrings.pyPython
name = "Aman"
score = 92

print(f"{name} scored {score} marks.")
print(f"Next target: {score + 5}")

Try f-strings →

Format numbers

f-string format specifiers numbers ko readable display dene me useful hain.

format-numbers.pyPython
price = 1999.5
ratio = 0.875
population = 1250000

print(f"₹{price:.2f}")
print(f"{ratio:.1%}")
print(f"{population:,}")
  • .2f — two decimal places
  • .1% — percentage with one decimal place
  • , — thousands separator

Basic alignment preview

alignment.pyPython
course = "Python"
print(f"|{course:<10}|")
print(f"|{course:^10}|")
print(f"|{course:>10}|")

<, ^, > left, center aur right alignment ke liye use kiye ja sakte hain.

Other formatting styles

str.format() aur old % formatting bhi Python codebases me mil sakte hain, but new beginner code me f-strings usually clearer choice hain.

format-method.pyPython
name = "Aman"
print("Hello, {}".format(name))

Understanding stdin in BrounStack Playground

Online runner me interactive typing ke bajay Program input / stdin box use hota hai. Har line sequential input() call ko feed hoti hai.

two-inputs.pyPython
name = input("Name: ")
age = int(input("Age: "))

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

Example stdin:

stdinInput
Aman
21

Run with two inputs →

Mini console program

bill.pyPython
item = input("Item: ")
price = float(input("Price: "))
quantity = int(input("Quantity: "))

total = price * quantity
print("-" * 24)
print(f"Item: {item}")
print(f"Quantity: {quantity}")
print(f"Total: ₹{total:.2f}")
Flow

Input lo → required type me convert karo → calculation karo → formatted output dikhao. Ye pattern bahut saare beginner console programs ka base hai.

print() for simple debugging

Learning ke waqt values inspect karne ke liye print() useful hai.

debug.pyPython
price = 200
quantity = 3
print("DEBUG:", price, quantity)

total = price * quantity
print(total)

Larger programs me proper debugger/logging tools better hote hain, but beginner experiments me print-debugging useful habit hai.

Common beginner mistakes

  • input() result ko number assume karna without conversion.
  • String aur integer ko + se directly combine karna.
  • Prompt unclear rakhna.
  • Formatting ke liye unnecessary string concatenation karna jab f-string clearer ho.
  • Currency/percentage output me suitable formatting na use karna.
  • Too many print() calls se output cluttered karna.
  • end="" use karke accidental missing line breaks create karna.
  • Backslash escape sequences bhoolna.
  • Invalid numeric input ke possibility ko ignore karna.

Beginner best practices

  • User ko clear prompt do.
  • Input ko required type me explicitly convert karo.
  • Output labels clear rakho.
  • Readable output ke liye f-strings prefer karo.
  • Money-like display me decimal places format karo, while remembering exact financial arithmetic is a separate concern.
  • Playground stdin me one input per line ka flow samjho.
  • Program ko input → process → output sections me mentally organize karo.

Chapter checklist

  • print() se single aur multiple values show kar sakte ho?
  • sep aur end ka use clear hai?
  • input() always text return karta hai — ye rule yaad hai?
  • Numeric input ko int()/float() se convert kar sakte ho?
  • f-string create kar sakte ho?
  • Decimal, percentage aur thousands formatting samajh aayi?
  • BrounStack stdin box se multiple inputs feed kar sakte ho?

Practice Task — Student Result Card

BrounStack Playground me ek console result card banao.

  1. Student name input lo.
  2. Course name input lo.
  3. Three subject marks as numbers lo.
  4. Total calculate karo.
  5. Average calculate karo.
  6. f-string se name aur course show karo.
  7. Total aur average labels ke saath print karo.
  8. Average ko two decimal places me format karo.
  9. sep use karke subject marks ek readable line me show karo.
  10. Decorative separator line print karo.
  11. At least one escape sequence use karo.
  12. Invalid text as marks dekar conversion error observe karo.
  13. Final clean version ko Playground me run karo.

Open practice starter →