Python input()
In this page:
variable = input("prompt message: ")
input() का बुनियादी उपयोग
input() आपके program को रोक देता है और इंतज़ार करता है कि user कुछ टाइप करके Enter दबाए; जो भी उन्होंने लिखा, वह string के रूप में लौटता है, चाहे वह संख्या जैसा दिखे या नहीं।
कमांड लाइन से script को interactive बनाने का यह Python का मानक तरीका है।
उदाहरण: Basic input() Usage
# name = input("Enter your name: ")
name = "Alex" # simulated input for this sandbox
print("Hello,", name)
Inputs को Integers में बदलना
क्योंकि input() हमेशा text लौटाता है, call को int() में लपेटना — जैसे age = int(input("Age: ")) — उस text को पूर्ण संख्या में बदल देता है ताकि आप उसे arithmetic या संख्यात्मक तुलनाओं में सुरक्षित रूप से इस्तेमाल कर सकें।
उदाहरण: Converting Inputs to Integers
# age = int(input("Age: "))
age = int("25")
print(age + 1)
Inputs को Floats में बदलना
दशमलव बिंदु वाले values के लिए, float() वही काम करता है जो int(), पर floating-point संख्या बनाता है, जो औसत या माप जैसी गणनाओं में मायने रखता है जहाँ पूर्ण संख्या सटीकता खो देगी।
उदाहरण: Converting Inputs to Floats
# price = float(input("Price: "))
price = float("19.99")
print(price * 2)
कई Inputs पढ़ना
input() से लौटी string पर .split() बुलाने से वह जहाँ भी whitespace हो वहाँ अलग-अलग टुकड़ों की list में बँट जाती है, जो एक ही line में user द्वारा टाइप किए कई values को पढ़ने का मानक तरीका है, जैसे x, y = input().split()।
उदाहरण: Reading Multiple Inputs
# x, y = input().split()
x, y = "3 4".split()
print(x, y)
Input Types को संभालना
अगर user वहाँ अक्षर टाइप कर दे जहाँ int() या float() संख्या की उम्मीद करता है, तो Python ValueError उठाती है और program क्रैश हो जाता है जब तक आप उसे पकड़ें नहीं — conversions को try/except block में लपेटना या validate करना हर उस program के लिए ज़रूरी है जो असली user input पर भरोसा करता है।
उदाहरण: Handling Input Types
text = "abc"
try:
number = int(text) # "abc" can't be parsed as an int
except ValueError:
print("That's not a valid number") # runs because the conversion failed
Chapter Quiz — Complete all 16 topics to unlock
0/16 topics done
Complete these topics first:
- Python print()
- Python input()
- Python Format Strings
- Python f-strings
- Python String Formatting
- Python Arithmetic Operators
- Python Relational Operators
- Python Logical Operators
- Python Bitwise Operators
- Python Assignment Operators
- Python Increment & Decrement
- Python Ternary Operator
- Python Operator Precedence
- Python Identity Operators
- Python Membership Operators
- Python Operators