← Back to Python Course | Chapter 2: Input, Output & Operators | Lesson 2 of 16

Python input()

input(), आपके प्रोग्राम को एक सवाल पूछने और व्यक्ति के जवाब टाइप करने का इंतज़ार करने देता है, जैसे कोई teacher आपका नाम पूछ रहा हो। आप जो भी टाइप करते हैं वह text के रूप में वापस आता है।
Syntax
python
variable = input("prompt message: ")

input() का बुनियादी उपयोग

input() आपके program को रोक देता है और इंतज़ार करता है कि user कुछ टाइप करके Enter दबाए; जो भी उन्होंने लिखा, वह string के रूप में लौटता है, चाहे वह संख्या जैसा दिखे या नहीं।

कमांड लाइन से script को interactive बनाने का यह Python का मानक तरीका है।

उदाहरण: Basic input() Usage

python
# 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

python
# age = int(input("Age: "))
age = int("25")
print(age + 1)

Inputs को Floats में बदलना

दशमलव बिंदु वाले values के लिए, float() वही काम करता है जो int(), पर floating-point संख्या बनाता है, जो औसत या माप जैसी गणनाओं में मायने रखता है जहाँ पूर्ण संख्या सटीकता खो देगी।

उदाहरण: Converting Inputs to Floats

python
# 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

python
# 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

python
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
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.