🖥 Python में Input और Output – User Interaction आसान तरीके से
Table of Contents
Toggle
किसी भी प्रोग्राम में Input का मतलब है user से data लेना और Output का मतलब है user को data दिखाना।
Python में यह काम input() और print() functions से किया जाता है।
इस guide में हम इन्हें आसान syntax, examples और tips के साथ समझेंगे।
📌 Quick Syntax:
# Input Example
name = input("Enter your name: ")
# Output Example
print("Hello,", name)
📝 Input Function
User से text या number लेने के लिए input() का इस्तेमाल करें।
📢 Output Function
User को message या data दिखाने के लिए print() का इस्तेमाल करें।
🎯 Formatting
Output को बेहतर दिखाने के लिए f-strings या format() method का उपयोग करें।
📝 Python में Input Function – Details और Examples
input() function का उपयोग user से data लेने के लिए किया जाता है। यह हमेशा string के रूप में data return करता है,
इसलिए अगर आपको number चाहिए तो type casting करना जरूरी है।
📌 Syntax:
# Syntax
variable_name = input("Prompt message: ")
Example 1: Simple Input
name = input("Enter your name: ")
print("Hello,", name)
Output में user का नाम दिखाया जाएगा।
Example 2: Type Casting
age = int(input("Enter your age: "))
print("Your age after 5 years:", age + 5)
यहां input को int() में convert किया गया है ताकि addition हो सके।
Example 3: Multiple Inputs in One Line
a, b = input("Enter two numbers separated by space: ").split()
print("First number:", a)
print("Second number:", b)
split() method का इस्तेमाल करके multiple values ली जा सकती हैं।
📝 Python में Input Function – Details और Examples
input() function का उपयोग user से data लेने के लिए किया जाता है। यह हमेशा string के रूप में data return करता है,
इसलिए अगर आपको number चाहिए तो type casting करना जरूरी है।
📌 Syntax:
# Syntax
variable_name = input("Prompt message: ")
Example 1: Simple Input
name = input("Enter your name: ")
print("Hello,", name)
Output में user का नाम दिखाया जाएगा।
Example 2: Type Casting
age = int(input("Enter your age: "))
print("Your age after 5 years:", age + 5)
यहां input को int() में convert किया गया है ताकि addition हो सके।
Example 3: Multiple Inputs in One Line
a, b = input("Enter two numbers separated by space: ").split()
print("First number:", a)
print("Second number:", b)
split() method का इस्तेमाल करके multiple values ली जा सकती हैं।
📢 Python Output — print() Explained (Formatting Tips सहित)
print() से हम स्क्रीन पर डेटा दिखाते हैं। इस सेक्शन में हम इसके parameters (sep, end, file, flush)
और formatting (f-strings, width/precision, alignment) सीखेंगे—ताकि python input output in hindi गाइड पूरी बन सके।
print("Hello", "Vista", 2025) # space-separated by default
print("Hello" + " " + "Vista") # manual concat
print("2025","08","11", sep="-") # 2025-08-11
print("Loading", end="...") # same line
print("done") # Loading...done
nums = [1,2,3]
print(*nums, sep=", ") # 1, 2, 3 (unpack)
print(" | ".join(map(str, nums)))# 1 | 2 | 3
name, price, pct = "Course", 2499.9, 0.075
print(f"{name}: ₹{price:.2f}") # ₹2499.90
print(f"Discount: {pct:.1%}") # 7.5%
print(f"|{'left':<10}|{'mid':^10}|{'right':>10}|")
print(f"{1234567:,}") # 1,234,567 (thousands sep)
# Legacy styles:
print("Hi {}, ₹{:.2f}".format("Vista", price))
print("Hi %s, ₹%.2f" % ("Vista", price))
import pprint, json
data = {"user":{"name":"A","skills":["py","sql"],"score":92}}
pprint.pp(data) # pretty for Python objects
print(json.dumps(data, indent=2)) # pretty JSON
# फ़ाइल में आउटपुट
with open("log.txt","w", encoding="utf-8") as f:
print("Hello File", file=f)
# तुरंत स्क्रीन पर फ्लश (e.g., progress)
import time
for i in range(3):
print(i, end=" ", flush=True); time.sleep(0.3)
print("Line1\nLine2") # newline escape
print(r"C:\Users\Vista") # raw string
print("नमस्ते 👋") # Unicode
print("""Multi-line
message with
line breaks""")
⚠️ print बनाम logging
Debug के लिए print() ठीक है; प्रोडक्शन में logging module बेहतर है (levels, files, handlers).
⚠️ sep/end भूलना
sep डिफ़ॉल्ट space है और end डिफ़ॉल्ट newline। एक ही लाइन पर प्रिंट के लिए end="" या कस्टम end यूज़ करें।
⚠️ प्रदर्शन
बहुत बड़ी लिस्ट/स्ट्रिंग को प्रिंट करने से पहले summarize करें; I/O धीमा हो सकता है।
🧪 10 Practice Problems — Input/Output (with Hints & Solutions)
इन problems से आपकी python input output in hindi पकड़ मज़बूत होगी। हर कार्ड में Hint और Solution (Copy-बटन) शामिल है।
P1. दो संख्याओं का जोड़
User से दो numbers लें और sum print करें।
💡 Hint
int(input()) और +
✅ Solution
a = int(input("Enter A: "))
b = int(input("Enter B: "))
print("Sum =", a + b)
P2. One-line Calculator
Input: num1 num2 → sum, diff, prod, div print करें।
💡 Hint
map(float, input().split())
✅ Solution
x, y = map(float, input("Enter two numbers: ").split())
print("Sum:", x+y, "Diff:", x-y, "Prod:", x*y, "Div:", x/y)
P3. Personalized Greeting
Name और City लें → f-string से output दें।
💡 Hint
f”Hello {name} from {city}”
✅ Solution
name = input("Your name: ")
city = input("Your city: ")
print(f"Hello {name} from {city}! 👋")
P4. N Numbers का Average
Input: n फिर अगली लाइन में n numbers → average।
💡 Hint
sum(nums)/len(nums)
✅ Solution
n = int(input("How many numbers? "))
nums = list(map(float, input("Enter numbers: ").split()))
print("Average =", sum(nums)/n)
P5. Km → Miles (formatted)
Km लें और miles (2 decimals) print करें।
💡 Hint
1 km = 0.621371 miles; :.2f
✅ Solution
km = float(input("Enter km: "))
miles = km * 0.621371
print(f"{km:.2f} km = {miles:.2f} miles")
P6. Simple Interest
Input: P, R, T → SI = P*R*T/100; nicely format करें।
💡 Hint
f"₹{si:.2f}"
✅ Solution
P = float(input("Principal: "))
R = float(input("Rate (%): "))
T = float(input("Time (years): "))
si = P*R*T/100
print(f"Simple Interest = ₹{si:.2f}")
P7. Even Numbers Filter
Input list → सिर्फ even numbers को space-separated print करें।
💡 Hint
print(*evens) या " ".join()
✅ Solution
nums = list(map(int, input("Enter numbers: ").split()))
evens = [n for n in nums if n % 2 == 0]
print(*evens)
P8. टेबल-जैसा Output (Alignment)
तीन items लें और aligned columns में print करें।
💡 Hint
{text:^10}, {num:>6}
✅ Solution
items = [input("Item1: "), input("Item2: "), input("Item3: ")]
print(f"|{'Item':^10}|{'Len':>6}|")
for it in items:
print(f"|{it:^10}|{len(it):>6}|")
P9. Valid Integer लो (त्रुटि-सुरक्षित)
जब तक user सही integer न दे, पूछते रहें।
💡 Hint
try/except ValueError
✅ Solution
while True:
s = input("Enter an integer: ")
try:
n = int(s)
break
except ValueError:
print("Invalid! Try again.")
print("You entered:", n)
P10. Progress Dots (end + flush)
एक लूप में dots दिखाएँ: ....done
💡 Hint
print(".", end="", flush=True)
✅ Solution
import time
for _ in range(5):
print(".", end="", flush=True); time.sleep(0.3)
print("done")
❓ Python Input/Output — FAQ (Hindi, 2025 Updated)
Q1. input() क्या वापस करता है? Number कैसे लें?
input() हमेशा string लौटाता है। Number लेने के लिए type casting करें:
int(input()), float(input()).
Q2. एक ही लाइन में multiple inputs कैसे लें?
a, b = input().split() या numbers के लिए:
a, b = map(int, input().split()).
Q3. print() में sep और end क्या करते हैं?
sep arguments के बीच का separator है (डिफ़ॉल्ट space) और end लाइन के अंत में जोड़ने वाला text है
(डिफ़ॉल्ट newline). जैसे: print(1,2,3, sep=",", end=";").
Q4. Output को formatted तरीके से कैसे दिखाएँ?
f-strings सबसे आसान हैं: f"Total: ₹{amount:.2f}". Alignment:
{text:^10}, {num:>6}; Thousands separator: {n:,}.
Q5. फ़ाइल में output कैसे लिखें? Encoding का ध्यान?
with open("out.txt","w", encoding="utf-8") as f: print("नमस्ते", file=f).
Unicode/हिंदी text के लिए encoding="utf-8" ज़रूर दें।
Q6. Console में progress/dots तुरंत कैसे दिखाएँ?
print(".", end="", flush=True) का उपयोग करें ताकि buffer तुरंत flush हो और output live दिखे।
🚀 अब आपकी बारी – Python Skills को अगले लेवल पर ले जाएँ!
आपने Python के input() और print() को अच्छे से सीख लिया है। अब आगे बढ़ें और File Handling, String Manipulation और Operators को मास्टर करें।
📂 Start Next: Python File Handling in Hindi