इस ट्यूटोरियल में, हम सीखेंगे कि पायथन में उपयोगकर्ता से कई इनपुट कैसे प्राप्त करें ।
उपयोगकर्ता द्वारा दर्ज किया गया डेटा स्ट्रिंग . में होगा प्रारूप। इसलिए, हम विभाजन () . का उपयोग कर सकते हैं उपयोगकर्ता द्वारा दर्ज किए गए डेटा को विभाजित करने की विधि।
आइए उपयोगकर्ता से कई तार लें।
उदाहरण
# taking the input from the user
strings = input("Enter multiple names space-separated:- ")
# spliting the data
strings = strings.split()
# printing the data
print(strings) आउटपुट
यदि आप उपरोक्त कोड चलाते हैं, तो आपको निम्न परिणाम प्राप्त होंगे।
Enter multiple names space-separated:- Python JavaScript Django React ['Python', 'JavaScript', 'Django', 'React']
क्या होगा अगर हम इनपुट के रूप में कई नंबर लेना चाहते हैं? हम मानचित्र . का उपयोग करके प्रत्येक इनपुट को एक पूर्णांक में बदल सकते हैं और इंट समारोह। आइए एक उदाहरण देखें।
उदाहरण
# taking the input from the user
numbers = input("Enter multiple numbers space-separated:- ")
# spliting the data and converting each string number to int
numbers = list(map(int, numbers.split()))
# printing the data
print(numbers) आउटपुट
यदि आप उपरोक्त कोड चलाते हैं, तो आपको निम्न परिणाम प्राप्त होंगे।
Enter multiple numbers space-separated:- 1 2 3 4 5 [1, 2, 3, 4, 5]
निष्कर्ष
आप अपनी आवश्यकताओं के अनुसार कोड को संशोधित कर सकते हैं और उपयोगकर्ताओं से इनपुट ले सकते हैं। यदि आप ट्यूटोरियल में प्रश्न पूछते हैं, तो उनका टिप्पणी अनुभाग में उल्लेख करें।