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.
Output with print()
print() values ko standard output par display karta hai.
print("Hello, BrounStack!")
print(42)
print(True)print() ke andar text, number, boolean ya variables de sakte ho. Python unhe readable form me console par show karta hai.
Print multiple values
print() multiple arguments accept karta hai. By default unke beech ek space insert hota hai.
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.
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.
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.
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.
name = input("Your name: ")
print("Hello,", name)input() returns a string
Important beginner rule: input() normally text yani str return karta hai, even if user digits type kare.
age = input("Age: ")
print(age)
print(type(age))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.
age = int(input("Age: "))
price = float(input("Price: "))
print(age + 1)
print(price * 2)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.
name = "Aman"
score = 92
print(f"{name} scored {score} marks.")
print(f"Next target: {score + 5}")Format numbers
f-string format specifiers numbers ko readable display dene me useful hain.
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
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.
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.
name = input("Name: ")
age = int(input("Age: "))
print(f"{name} will be {age + 1} next year.")Example stdin:
Aman
21Mini console program
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}")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.
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?sepaurendka 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.
- Student name input lo.
- Course name input lo.
- Three subject marks as numbers lo.
- Total calculate karo.
- Average calculate karo.
- f-string se name aur course show karo.
- Total aur average labels ke saath print karo.
- Average ko two decimal places me format karo.
sepuse karke subject marks ek readable line me show karo.- Decorative separator line print karo.
- At least one escape sequence use karo.
- Invalid text as marks dekar conversion error observe karo.
- Final clean version ko Playground me run karo.