Python Input and Output Examples
Python provides simple ways to take user input and display output. Here are 10 examples demonstrating input(), print(), and formatting techniques including reading different data types.
Python उपयोगकर्ता से इनपुट लेने और आउटपुट दिखाने के आसान तरीके देता है। यहाँ 10 उदाहरण दिए गए हैं जो input(), print() और formatting को दिखाते हैं, साथ ही अलग-अलग data types पढ़ना भी समझाते हैं।
Example 1: Basic print()
print('Hello, World!')
Output:
Prints simple text output.
साधारण टेक्स्ट स्क्रीन पर दिखाता है।
Example 2: Print multiple values
print('Sum of', 2, 'and', 3, 'is', 2 + 3)
Output:
Print multiple items separated by spaces.
कई आइटम को स्पेस से अलग करके दिखाता है।
Example 3: Using input() to read string
name = input('Enter your name: ')
print('Hello', name)
Input Prompt:
Output:
Reads a string from user and prints greeting.
यूज़र से नाम लेता है और स्वागत करता है।
Example 4: Convert input to integer
age = int(input('Enter your age: '))
print('You are', age, 'years old.')
Input Prompt:
Output:
Reads input, converts to int, then prints.
इनपुट को integer में बदलकर दिखाता है।
Example 5: Convert input to float
price = float(input('Enter product price: '))
print('Price is', price)
Input Prompt:
Output:
Reads input, converts to float, then prints.
इनपुट को float में बदलकर दिखाता है।
Example 6: Formatted output with f-string
name = 'Aman'
age = 25
print(f'{name} is {age} years old.')
Output:
Uses f-string for formatted output.
f-string से फॉर्मेटेड आउटपुट दिखाता है।
Example 7: Print without newline
print('Hello', end=' ')
print('World!')
Output:
Print multiple items on the same line.
एक ही लाइन में कई चीजें प्रिंट करता है।
Example 8: Using sep parameter
print('Python', 'is', 'fun', sep='-')
Output:
Changes separator between printed items.
प्रिंट किए गए आइटम के बीच अलग separator लगाता है।
Example 9: Reading multiple inputs
x, y = input('Enter two numbers separated by space: ').split()
print('Sum:', int(x) + int(y))
Input Prompt:
Output:
Reads multiple inputs separated by space and sums them.
स्पेस से अलग दो इनपुट लेता है और जोड़ता है।
Example 10: Escape characters in output
print('Hello\nWorld!')
Output:
World!
Prints output with newline using escape character.
न्यू लाइन के लिए escape character का उपयोग करता है।